name
stringlengths
18
69
filepath
stringclasses
38 values
source
stringclasses
38 values
test
stringlengths
306
10.4k
LiteralUtilTest_SliceR1F32
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, SliceR1F32) { auto input = LiteralUtil::CreateR1<float>({1.0, 2.0, 3.0, 4.0, 5.0}); auto result = input.Slice({3}, {4}); auto expected = LiteralUtil::CreateR1<float>({4.0}); EXPECT_EQ(expected, result); }
LiteralUtilTest_SliceR2Dynamic
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, SliceR2Dynamic) { auto input_3x4 = LiteralUtil::CreateR2<uint32_t>( {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}); input_3x4.SetDynamicSize(1, 3); // slice second dim from dynamic size 3 to dynamic size 1. auto result = input_3x4.Slice({0, 1}, {2, 2}); auto expected = LiteralUtil::CreateR2<uint32_t>({{2}, {6}}); EXPECT_EQ(expected, result); EXPECT_EQ(result.GetDynamicSize(1), 1); }
LiteralUtilTest_SliceR2DynamicInBound
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, SliceR2DynamicInBound) { auto input_3x4 = LiteralUtil::CreateR2<uint32_t>( {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}); input_3x4.SetDynamicSize(1, 1); auto result = input_3x4.Slice({0, 0}, {2, 2}); auto expected = LiteralUtil::CreateR2<uint32_t>({{1}, {5}}); EXPECT_EQ(expected, result); EXPECT_EQ(result.GetDynamicSize(1), 1); }
LiteralUtilTest_SliceR2DynamicOutOfBound
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, SliceR2DynamicOutOfBound) { auto input_3x4 = LiteralUtil::CreateR2<uint32_t>( {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}); input_3x4.SetDynamicSize(1, 1); auto result = input_3x4.Slice({0, 1}, {2, 3}); auto expected = LiteralUtil::CreateR2<uint32_t>({{}, {}}); EXPECT_EQ(expected, result); // Out of bound access clamps into 0 sized dimension. EXPECT_EQ(result.GetDynamicSize(1), 0); }
LiteralUtilTest_SliceR2U32
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, SliceR2U32) { auto input_3x4 = LiteralUtil::CreateR2<uint32_t>( {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}); auto result = input_3x4.Slice({0, 2}, {2, 4}); auto expected = LiteralUtil::CreateR2<uint32_t>({{3, 4}, {7, 8}}); EXPECT_EQ(expected, result); }
LiteralUtilTest_SliceR3U32Full
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; };
LiteralUtilTest_TestR2LinearLayout
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, TestR2LinearLayout) { // Test expected memory layout of R2 dim0-minor (column-major) literal. auto mat_dim0minor = LiteralUtil::CreateR2WithLayout<int32_t>( {{1, 2, 3}, {4, 5, 6}}, layout_r2_dim0minor_); EXPECT_EQ(mat_dim0minor.element_count(), 6); EXPECT_THAT(mat_dim0minor.data<int32_t>(), ElementsAre(1, 4, 2, 5, 3, 6)); // Test expected memory layout when using Relayout to row major. auto relaid_mat_to_dim0major = mat_dim0minor.Relayout(layout_r2_dim0major_); EXPECT_THAT(relaid_mat_to_dim0major.data<int32_t>(), ElementsAre(1, 2, 3, 4, 5, 6)); EXPECT_EQ(absl::HashOf(HashTester<false>{&mat_dim0minor}), absl::HashOf(HashTester<false>{&relaid_mat_to_dim0major})); // Test expected memory layout of R2 created with dim0-major (row-major). auto mat_dim0major = LiteralUtil::CreateR2WithLayout<int32_t>( {{1, 2, 3}, {4, 5, 6}}, layout_r2_dim0major_); EXPECT_EQ(mat_dim0major.element_count(), 6); EXPECT_THAT(mat_dim0major.data<int32_t>(), ElementsAre(1, 2, 3, 4, 5, 6)); // Test expected memory layout when using Relayout to column major. auto relaid_mat_to_dim0minor = mat_dim0major.Relayout(layout_r2_dim0minor_); EXPECT_THAT(relaid_mat_to_dim0minor.data<int32_t>(), ElementsAre(1, 4, 2, 5, 3, 6)); EXPECT_EQ(absl::HashOf(HashTester<false>{&mat_dim0major}), absl::HashOf(HashTester<false>{&relaid_mat_to_dim0minor})); // Test that layout sensitive hashes are equal. EXPECT_EQ(absl::HashOf(HashTester<true>{&mat_dim0minor}), absl::HashOf(HashTester<true>{&relaid_mat_to_dim0minor})); EXPECT_EQ(absl::HashOf(HashTester<true>{&mat_dim0major}), absl::HashOf(HashTester<true>{&relaid_mat_to_dim0major})); }
LiteralUtilTest_TestR3LinearLayout
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; };
LiteralUtilTest_TestR4RelayoutEquivalence
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, TestR4RelayoutEquivalence) { // Tests that using Relayout on an array is equivalent to creating it in the // target layout in the first place. auto dim0minor_relaid_to_dim0major = literal_r4_2x2x3x3_dim0minor_.Relayout(layout_r4_dim0major_); EXPECT_EQ(literal_r4_2x2x3x3_dim0major_, dim0minor_relaid_to_dim0major); auto dim0major_relaid_to_dim0minor = literal_r4_2x2x3x3_dim0major_.Relayout(layout_r4_dim0minor_); EXPECT_EQ(literal_r4_2x2x3x3_dim0minor_, dim0major_relaid_to_dim0minor); }
LiteralUtilTest_ToBoundedDynamicR2
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, ToBoundedDynamicR2) { // F32[2, 1] auto original = LiteralUtil::CreateR2<float>({{1}, {4}}); // F32[2, <=3] (2, 1) auto dynamic_shape = ShapeUtil::MakeShape(F32, {2, 3}, {false, true}); auto dynamic_literal = original.ToBoundedDynamic(dynamic_shape); EXPECT_EQ(dynamic_literal.shape(), dynamic_shape); dynamic_literal.EachCell<float>( [&](absl::Span<const int64_t> indices, float value) { EXPECT_EQ(value, original.Get<float>({indices[0], indices[1]})); }); }
LiteralUtilTest_TokenEquality
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, TokenEquality) { auto token0 = LiteralUtil::CreateToken(); auto token1 = LiteralUtil::CreateToken(); auto scalar = LiteralUtil::CreateR0<float>(1.0); EXPECT_EQ(token0, token1); EXPECT_NE(token0, scalar); EXPECT_EQ(LiteralUtil::MakeTuple({&token0}), LiteralUtil::MakeTuple({&token0})); EXPECT_EQ(LiteralUtil::MakeTuple({&token0, &scalar}), LiteralUtil::MakeTuple({&token1, &scalar})); EXPECT_NE(LiteralUtil::MakeTuple({&token0, &scalar}), LiteralUtil::MakeTuple({&scalar, &token1})); }
LiteralUtilTest_ToStaticR2
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, ToStaticR2) { // F32[2, <=3] (2, 1) auto original = LiteralUtil::CreateR2<float>({{1, 2, 3}, {4, 5, 6}}); original.SetDynamicSize(1, 1); // F32[2, 1] auto static_literal = original.ToStatic(); EXPECT_EQ(static_literal.shape(), ShapeUtil::MakeShape(F32, {2, 1})); EXPECT_TRUE(static_literal.shape().is_static()); static_literal.EachCell<float>( [&](absl::Span<const int64_t> indices, float value) { EXPECT_EQ(value, original.Get<float>({indices[0], indices[1]})); }); }
LiteralUtilTest_TransposeDynamicR2
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, TransposeDynamicR2) { // F32[2, <=3] (2, 1) auto original = LiteralUtil::CreateR2<float>({{1, 2, 3}, {4, 5, 6}}); original.SetDynamicSize(1, 1); // F32[<=3, 2] (1, 2) auto reshape = original.Transpose(/*permutation=*/{1, 0}); reshape.EachCell<float>([&](absl::Span<const int64_t> indices, float value) { EXPECT_EQ(value, original.Get<float>({indices[1], indices[0]})); }); }
LiteralUtilTest_TransposeR0
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, TransposeR0) { auto original = LiteralUtil::CreateR0<float>(1.7f); auto reshape = original.Transpose(/*permutation=*/{}); EXPECT_EQ(original, reshape); }
LiteralUtilTest_TransposeR4
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; };
LiteralUtilTest_TupleEquality
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, TupleEquality) { // Test equality with tuples. auto scalar = LiteralUtil::CreateR0<float>(1.0); auto matrix = LiteralUtil::CreateR2<float>({{1.0, 2.0}, {3.0, 4.0}}); auto tuple1 = LiteralUtil::MakeTuple({&scalar, &matrix}); // Tuple with the same elements. One element is shared with the original // tuple, the other is a clone of the element in the original tuple. auto scalar_clone = LiteralUtil::CreateR0<float>(1.0); auto tuple2 = LiteralUtil::MakeTuple({&scalar_clone, &matrix}); EXPECT_EQ(tuple1, tuple2); // Tuple with elements reversed. auto reversed_tuple = LiteralUtil::MakeTuple({&matrix, &scalar}); EXPECT_NE(tuple1, reversed_tuple); // Tuple with different value. auto scalar_42 = LiteralUtil::CreateR0<float>(42.0); auto different_tuple = LiteralUtil::MakeTuple({&scalar_42, &matrix}); EXPECT_NE(tuple1, different_tuple); }
LiteralUtilTest_TupleToString
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, TupleToString) { auto scalar = LiteralUtil::CreateR0<float>(1.0); auto matrix = LiteralUtil::CreateR2<float>({{1.0, 2.0}, {3.0, 4.0}}); auto tuple = LiteralUtil::MakeTuple({&scalar, &matrix}); const std::string expected = R"(( f32[] 1, f32[2,2] { { 1, 2 }, { 3, 4 } } ))"; EXPECT_EQ(expected, tuple.ToString()); }
LiteralUtilTest_ValidProtoNoValues
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, ValidProtoNoValues) { // Proto contains a shape, but no values. LiteralProto proto; *proto.mutable_shape() = ShapeUtil::MakeShape(F32, {3}).ToProto(); absl::Status status = Literal::CreateFromProto(proto, /*prohibit_empty_literal=*/false) .status(); EXPECT_TRUE(status.ok()); }
LiteralUtilTest_ValidProtoWithClearedValues
xla/literal_test.cc
bool LiteralProtoHasValues(const LiteralProto& proto) { return !proto.s2s().empty() || !proto.s4s().empty() || !proto.s8s().empty() || !proto.s16s().empty() || proto.s32s_size() || proto.s64s_size() || !proto.u2s().empty() || !proto.u4s().empty() || !proto.u8s().empty() || !proto.u16s().empty() || proto.u32s_size() || proto.u64s_size() || !proto.f8e5m2s().empty() || !proto.f8e4m3fns().empty() || !proto.f8e4m3b11fnuzs().empty() || !proto.f8e5m2fnuzs().empty() || !proto.f8e4m3fnuzs().empty() || !proto.f16s().empty() || !proto.bf16s().empty() || proto.f32s_size() || proto.f64s_size() || proto.c64s_size() || proto.c128s_size() || proto.preds_size() || proto.tuple_literals_size(); } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } const Shape& ScalarShapeImpl() { static_assert(primitive_util::IsArrayType(kType), "Not a valid type for a scalar."); static const Shape* shape = [] { auto shape = new Shape(kType, {}, {}, {}); shape->mutable_layout(); return shape; }(); return *shape; } static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { static const Shape* shape = [] { const Shape& NilShape() { static const Shape* shape = new Shape(TUPLE, {}, {}, {}); return *shape; } const Shape* TryInternShape(const Shape& shape) { if (shape.IsTuple() && shape.tuple_shapes_size() == 0) { return &NilShape(); } if (shape.IsArray() && shape.dimensions_size() == 0 && shape.is_static() && shape.layout().tiles_size() == 0 && shape.layout().memory_space() == 0) { return &ScalarShape(shape.element_type()); } return nullptr; } StrideConfig::StrideConfig(const Shape& source_shape, const Shape& dest_shape, absl::Span<const int64_t> dimensions) : dimensions(dimensions), base(dimensions.size(), 0), step(dimensions.size(), 1) { if (!dimensions.empty()) { // Selects the shape with the largest minor dimension as the one upon // which to run the tight stride loop. if (dimensions[LayoutUtil::Minor(source_shape.layout(), 0)] >= dimensions[LayoutUtil::Minor(dest_shape.layout(), 0)]) { minor_dimension = LayoutUtil::Minor(source_shape.layout(), 0); dest_stride = IndexUtil::GetDimensionStride(dest_shape, minor_dimension); } else { minor_dimension = LayoutUtil::Minor(dest_shape.layout(), 0); source_stride = IndexUtil::GetDimensionStride(source_shape, minor_dimension); } minor_loop_size = dimensions[minor_dimension]; step[minor_dimension] = minor_loop_size; } } LiteralBase::~LiteralBase() = default; const Shape& LiteralBase::shape() const { return root_piece().subshape(); } const char* LiteralBase::Piece::buffer() const { // std::visit is avoided here due to its code size issues. if (auto* r = std::get_if<DenseRep>(&rep_)) { return r->data; } if (auto* r = std::get_if<DenseInlinedRep>(&rep_)) { return r->data; } DCHECK(std::holds_alternative<TupleRep>(rep_) || std::holds_alternative<Uninitialized>(rep_)); return nullptr; } const LiteralBase::Piece& LiteralBase::piece( const ShapeIndex& shape_index) const { const Piece* piece = &root_piece(); for (const auto i : shape_index) { DCHECK_GE(i, 0); DCHECK_LT(i, piece->children_size()); piece = &piece->child(i); } return *piece; } std::ostream& operator<<(std::ostream& out, const Literal& literal) { out << literal.ToString(); return out; } Shape* MutableLiteralBase::mutable_shape_do_not_use() { const Shape* const_shape = shape_.get(); if (!shape_.OwnsPtr()) { shape_ = MaybeOwningShapePtr(std::make_unique<Shape>(*shape_)); } Shape* shape = shape_.get_mutable(); if (shape != const_shape) { std::function<void(const Shape&, Piece*)> set_piece_shapes = [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; set_piece_shapes(*shape, &mutable_root_piece()); } return shape; } [&set_piece_shapes](const Shape& shape, Piece* piece) { piece->set_subshape(&shape); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); set_piece_shapes(subshape, &piece->child(i)); } } }; Literal::Literal() : Literal(NilShape()) {} Literal::Literal(const Shape& shape) : Literal(shape, /*allocate_arrays=*/true) {} void Literal::SetShape(const Shape& shape) { Shape shape_storage; const Shape* shape_ptr = &shape; if (LayoutUtil::HasCustomElementSizeInBits(shape)) { shape_storage = shape; shape_storage.mutable_layout()->set_element_size_in_bits(0); shape_ptr = &shape_storage; } if (const Shape* intered_shape_ptr = TryInternShape(*shape_ptr)) { shape_ = intered_shape_ptr; } else { shape_ = std::make_unique<Shape>(*shape_ptr); } } void Literal::SetPiece(const Shape& shape, Piece* piece, bool allocate_arrays, ArrayValueState leaf_array_value_state) { if (shape.IsTuple()) { for (const Shape& subshape : shape.tuple_shapes()) { Piece child_piece; child_piece.set_subshape(&subshape); SetPiece(subshape, &child_piece, allocate_arrays, leaf_array_value_state); piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { DCHECK(LayoutUtil::IsDenseArray(shape)) << "literal array storage is currently only supported for dense " "arrays: " << shape; piece->set_array_value_state(leaf_array_value_state); if (leaf_array_value_state == LiteralBase::ArrayValueState::kKnown && allocate_arrays) { piece->AllocateBuffers(); } } } Literal::Literal(const Shape& shape, bool allocate_arrays, ArrayValueState leaf_array_value_state) : MutableLiteralBase() { SetShape(shape); CHECK(leaf_array_value_state != ArrayValueState::kKnown || LayoutUtil::HasLayout(*shape_)); root_piece_.set_subshape(shape_.get()); CHECK(&root_piece_.subshape() == shape_.get()); SetPiece(*shape_, &root_piece_, allocate_arrays, leaf_array_value_state); } Literal::~Literal() { DeallocateBuffers(); } void Literal::DeallocateBuffers() { root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { piece->DeallocateBuffers(); }); } Literal::Literal(Literal&& other) : MutableLiteralBase() { *this = std::move(other); } Literal& Literal::operator=(Literal&& other) { DCHECK(&other.root_piece_.subshape() == other.shape_.get()); using std::swap; swap(shape_, other.shape_); swap(root_piece_, other.root_piece_); DCHECK(&root_piece_.subshape() == shape_.get()); return *this; } Literal LiteralBase::CreateFromShape(const Shape& shape) { Literal literal(shape); literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); return literal; } [&](const ShapeIndex& index, Piece* piece) { if (piece->subshape().IsArray()) { memset(piece->untyped_data(), 0, piece->size_bytes_dense()); } }); Literal LiteralBase::CreateFromShapeWithUnknownLeafArrays(const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUnknown); return literal; } Literal LiteralBase::CreateFromShapeWithUndeterminedLeafArrays( const Shape& shape) { Literal literal(shape, /*allocate_arrays=*/false, ArrayValueState::kUndetermined); return literal; } int32_t LiteralBase::GetDynamicSize(int64_t dim_index) const { return GetDynamicSize(dim_index, {}); } int32_t LiteralBase::GetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index) const { return piece(shape_index).GetDynamicSize(dim_index); } std::optional<int64_t> LiteralBase::GetFirstInteger() const { if (!primitive_util::IsIntegralType(shape().element_type())) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; auto first_element = GetFirstElement<NativeT>(); if constexpr (std::is_same_v<NativeT, uint64_t>) { int64_t v = static_cast<int64_t>(first_element); if (v < 0) { return std::nullopt; } } return first_element; }, void LiteralBase::BuildPieceSubtree(const Shape& shape, Piece* piece) { CHECK(shape.IsTuple()); for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); if (subshape.IsTuple()) { BuildPieceSubtree(subshape, &child_piece); } piece->emplace_back(std::move(child_piece)); } } absl::Status LiteralBase::SerializeToString(std::string* output) const { ShapeProto shape_proto = shape().ToProto(); TF_ASSIGN_OR_RETURN(int64_t size, ShapeUtil::SerializedSizeWithProto(shape(), shape_proto)); output->resize(size); return SerializeWithShapeProto(shape_proto, output->data()); } absl::StatusOr<std::string> LiteralBase::SerializeAsString() const { std::string result; TF_RETURN_IF_ERROR(SerializeToString(&result)); return std::move(result); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } absl::Status MutableLiteralBase::CopySliceFromInternal( const LiteralBase& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { auto linear_index = [](const Shape& shape, absl::Span<const int64_t> multi_index) { return IndexUtil::MultidimensionalIndexToLinearIndex(shape, multi_index); }; // `this->` is needed to workaround MSVC bug: #16882 NativeT* dest_data = this->data<NativeT>().data(); const NativeT* src_data = src_literal.data<NativeT>().data(); if (src_literal.shape().rank() == 0 || shape().rank() == 0) { // If any of the two shapes are scalars, just assign the value once. TF_RET_CHECK(copy_size.empty()); dest_data[linear_index(shape(), dest_base)] = src_data[linear_index(src_literal.shape(), src_base)]; } else if (!ShapeUtil::IsZeroElementArray(shape()) && !ShapeUtil::IsZeroElementArray(src_literal.shape()) && absl::c_none_of(copy_size, [](auto d) { return d == 0; })) { // Perform copy if none of src, dest and copy_size has dimensions with zero // element, otherwise it's a no-op. TF_RET_CHECK(src_base.size() == dest_base.size()); TF_RET_CHECK(src_base.size() == copy_size.size()); // Scan the source from minor, stepping in copy size blocks, then within // the index enumeration functor, do a strided copy advancing source index // by one (walking through the minor dimension), and destination index by // proper stride size at the matching dimension. DimensionVector src_indexes(src_base.size(), 0); DimensionVector dest_indexes(dest_base.size(), 0); StrideConfig stride_config(src_literal.shape(), shape(), copy_size); auto copy_proc = [&](absl::Span<const int64_t> indexes) { // Map from multi-dimensional index, to source index. std::transform(indexes.begin(), indexes.end(), src_base.begin(), src_indexes.begin(), std::plus<int64_t>()); // Map from multi-dimensional index, to destination index. std::transform(indexes.begin(), indexes.end(), dest_base.begin(), dest_indexes.begin(), std::plus<int64_t>()); int64_t src_index = linear_index(src_literal.shape(), src_indexes); int64_t dest_index = linear_index(shape(), dest_indexes); StridedCopy(dest_data + dest_index, stride_config.dest_stride, src_data + src_index, stride_config.source_stride, stride_config.minor_loop_size); return true; }; ShapeUtil::ForEachIndex(src_literal.shape(), stride_config.base, stride_config.dimensions, stride_config.step, copy_proc); } return absl::OkStatus(); } auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { auto copy_proc = [&](absl::Span<const int64_t> indexes) { void MutableLiteralBase::CopyElementFrom(const LiteralSlice& src_literal, absl::Span<const int64_t> src_index, absl::Span<const int64_t> dest_index) { DCHECK(LayoutUtil::IsDenseArray(shape())); DCHECK_EQ(shape().element_type(), src_literal.shape().element_type()); const int64_t src_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(src_literal.shape(), src_index); const int64_t dest_linear_index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), dest_index); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); char* dest_address = static_cast<char*>(untyped_data()) + dest_linear_index * primitive_size; const char* source_address = static_cast<const char*>(src_literal.untyped_data()) + src_linear_index * primitive_size; if (dest_address != source_address) { memcpy(dest_address, source_address, primitive_size); } } /* static */ absl::StatusOr<Literal> MutableLiteralBase::CreateFromProto( const LiteralProto& proto, bool prohibit_empty_literal) { if (!proto.has_shape()) { return InvalidArgument("LiteralProto has no shape"); } Shape shape(proto.shape()); if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { return InvalidArgument( "Literal shape cannot include OPAQUE_TYPE sub-shape"); } if (!LayoutUtil::HasLayout(shape)) { return InvalidArgument("LiteralProto has no layout"); } if (LayoutUtil::IsSparseArray(shape)) { return Unimplemented("Sparse literals are not supported"); } TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); Literal literal(shape); TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) -> absl::Status { const LiteralProto* proto_element = &proto; for (int64_t i : index) { CHECK(i < proto_element->tuple_literals_size()); proto_element = &proto_element->tuple_literals(i); } if (piece->subshape().IsTuple()) { if (proto_element->tuple_literals_size() != ShapeUtil::TupleElementCount(piece->subshape())) { return InvalidArgument( "Expected %d tuple elements in LiteralProto, has %d", ShapeUtil::TupleElementCount(piece->subshape()), proto_element->tuple_literals_size()); } return absl::OkStatus(); } if (piece->subshape().element_type() == TOKEN) { return absl::OkStatus(); } CHECK(piece->subshape().IsArray()); // When prohibit_empty_literal is false (allowing literal with no // values), only copy from proto if the literal proto has values. This // mode is used for a learned cost model. if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); } return absl::OkStatus(); })); return std::move(literal); } TF_RETURN_IF_ERROR(literal.root_piece_.ForEachMutableSubpieceWithStatus( Literal Literal::SubLiteral(ShapeIndexView shape_index) { if (!shape_index.empty()) { auto decomposed = this->DecomposeTuple(); return decomposed.at(shape_index.front()) .SubLiteral(shape_index.subspan(1)); } else { return std::move(*this); } } std::vector<Literal> Literal::DecomposeTuple() { CHECK(shape().IsTuple()); std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { elements.push_back(Literal(ShapeUtil::GetSubshape(shape(), {i}), /*allocate_arrays=*/false)); Literal& element = elements.back(); element.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); } // Set this literal to be nil-shaped. *this = Literal(); return elements; } [&](const ShapeIndex& index, Piece* dest_piece) { if (dest_piece->subshape().IsTuple()) { return; } ShapeIndex src_index = {i}; for (int64_t j : index) { src_index.push_back(j); } Piece& src_piece = piece(src_index); // Move the respective buffer over to the element Literal. dest_piece->MoveDataFrom(src_piece); }); void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } void CopyElementsBetween(absl::Span<NativeT> dest, absl::Span<const NativeT> src, const Shape& dest_shape, const Shape& src_shape) { DCHECK(LayoutUtil::IsDenseArray(dest_shape)); DCHECK(LayoutUtil::IsDenseArray(src_shape)); DCHECK(ShapeUtil::Compatible(dest_shape, src_shape)); if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } std::vector<int64_t> index(dest_shape.rank()); do { dest[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src[IndexUtil::MultidimensionalIndexToLinearIndex(src_shape, index)]; } while (IndexUtil::BumpIndices(dest_shape, absl::MakeSpan(index))); } int32_t LiteralBase::Piece::GetDynamicSize(int64_t dim_index) const { CHECK(LayoutUtil::IsDenseArray(subshape())); if (!subshape_->is_dynamic_dimension(dim_index)) { // This is a static dimension, return size. return subshape_->dimensions(dim_index); } return dynamic_size_buffer()[dim_index]; } void LiteralBase::Piece::SetDynamicSize(int64_t dim_index, int32_t size) { CHECK(LayoutUtil::IsDenseArray(subshape())); CHECK(subshape_->is_dynamic_dimension(dim_index)); dynamic_size_buffer()[dim_index] = size; } void LiteralBase::Piece::AllocateBuffers() { const int64_t bytes = total_bytes_dense(); if (bytes > kMaxInlinedBytes) { CHECK_EQ(buffer(), nullptr); rep_.emplace<DenseRep>(); set_buffer( static_cast<char*>(tsl::port::AlignedMalloc(bytes, kMinimumAlignment))); } else { rep_.emplace<DenseInlinedRep>(); } } void LiteralBase::Piece::DeallocateBuffers() { if (auto* array_rep = GetDenseRep()) { tsl::port::AlignedFree(array_rep->data); rep_.emplace<Uninitialized>(); } } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } void LiteralBase::Piece::CopyElementsWithDynamicBound( const LiteralBase::Piece& src) { auto& dest_shape = subshape(); auto& src_shape = src.subshape(); // At least one shape has to be static as bound. CHECK(dest_shape.is_static() || src_shape.is_static()); auto& bound_shape = dest_shape.is_static() ? src_shape : dest_shape; if (ShapeUtil::IsZeroElementArray(dest_shape)) { return; } if (dest_shape.rank() == 1) { // Fast path for rank 1 arrays. int64_t count = std::min(GetDynamicSize(0), src.GetDynamicSize(0)); std::copy_n(src.data<NativeT>().begin(), count, data<NativeT>().begin()); return; } std::vector<int64_t> index(dest_shape.rank()); do { bool out_of_bound = false; for (int64_t i = 0; i < index.size(); ++i) { // Do not copy elements beyond dynamic bound. if (index[i] >= GetDynamicSize(i) || index[i] >= src.GetDynamicSize(i)) { out_of_bound = true; } } if (out_of_bound) { continue; } data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex(dest_shape, index)] = src.data<NativeT>()[IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, index)]; } while (IndexUtil::BumpIndices(bound_shape, absl::MakeSpan(index))); } absl::Status LiteralBase::Piece::CopyFrom(const LiteralBase::Piece& src, bool only_dynamic_bound) { CHECK(subshape_ != nullptr); CHECK(src.subshape_ != nullptr); CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK(LayoutUtil::IsDenseArray(src.subshape())) << __func__ << " is only supported for dense arrays: " << src.subshape(); if (!only_dynamic_bound) { CHECK(ShapeUtil::Compatible(subshape(), src.subshape())); } if (src.array_value_state_ == ArrayValueState::kUnknown || src.array_value_state_ == ArrayValueState::kUndetermined) { if (array_value_state_ == ArrayValueState::kKnown) { DeallocateBuffers(); } array_value_state_ = src.array_value_state_; return absl::OkStatus(); } else { CHECK(src.array_value_state_ == ArrayValueState::kKnown); if (array_value_state_ == ArrayValueState::kUndetermined || array_value_state_ == ArrayValueState::kUnknown) { AllocateBuffers(); } array_value_state_ = src.array_value_state_; } if (ShapeUtil::Equal(subshape(), src.subshape())) { // If the layouts are equal it's faster just to memcpy. memcpy(buffer(), src.buffer(), src.size_bytes_dense()); } else { std::vector<int64_t> origin(subshape().rank(), 0); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, subshape().element_type()); } DCHECK_EQ(dynamic_size_buffer_bytes(), src.dynamic_size_buffer_bytes()); if (subshape().is_dynamic() && src.subshape().is_dynamic()) { memcpy(dynamic_size_buffer(), src.dynamic_size_buffer(), src.dynamic_size_buffer_bytes()); } return absl::OkStatus(); } [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, [&](auto primitive_type_constant) { using NativeT = NativeTypeOf<primitive_type_constant>; if (only_dynamic_bound) { CopyElementsWithDynamicBound<NativeT>(src); } else { CopyElementsBetween<NativeT>(this->data<NativeT>(), src.data<NativeT>(), subshape(), src.subshape()); } }, void MutableLiteralBase::SetDynamicSize(int64_t dim_index, int32_t size) { return SetDynamicSize(dim_index, {}, size); } void MutableLiteralBase::SetDynamicSize(int64_t dim_index, const ShapeIndex& shape_index, int32_t size) { Shape* subshape = ShapeUtil::GetMutableSubshape(mutable_shape_do_not_use(), shape_index); CHECK(LayoutUtil::IsDenseArray(*subshape)) << __func__ << " is only supported for dense arrays: " << *subshape; CHECK_GE(subshape->dimensions(dim_index), size); subshape->set_dynamic_dimension(dim_index, true); CHECK_EQ(&piece(shape_index).subshape(), subshape); piece(shape_index).SetDynamicSize(dim_index, size); } absl::Status MutableLiteralBase::CopyFrom(const LiteralSlice& src_literal, const ShapeIndex& dest_shape_index, const ShapeIndex& src_shape_index, bool only_dynamic_bound) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); const Shape& src_subshape = ShapeUtil::GetSubshape(src_literal.shape(), src_shape_index); if (only_dynamic_bound) { auto& bound_shape = dest_subshape.is_static() ? src_subshape : dest_subshape; auto& compact_shape = dest_subshape.is_static() ? dest_subshape : src_subshape; CHECK(ShapeUtil::DynamicShapeIsCompatible(compact_shape, bound_shape)) << compact_shape.ToString() << " vs " << bound_shape.ToString(); } else { if (!ShapeUtil::Compatible(dest_subshape, src_subshape)) { return InvalidArgument( "Destination subshape incompatible with source subshape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_subshape)); } } return mutable_root_piece().ForEachMutableSubpieceWithStatus( [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); } [&](const ShapeIndex& index, Piece* piece) { if (!piece->subshape().IsArray()) { return absl::OkStatus(); } // Determine if this index is in the part of this literal that we want // to copy over from src_literal. bool in_subtree_to_copy = true; for (int i = 0; i < dest_shape_index.size(); ++i) { if (index[i] != dest_shape_index[i]) { in_subtree_to_copy = false; break; } } if (!in_subtree_to_copy) { return absl::OkStatus(); } // Construct the index of the corresponding piece in the source literal. ShapeIndex src_piece_index = src_shape_index; for (int64_t i = dest_shape_index.size(), end = index.size(); i < end; ++i) { src_piece_index.push_back(index[i]); } TF_RETURN_IF_ERROR( piece->CopyFrom(src_literal.piece(src_piece_index), /*only_dynamic_bound=*/only_dynamic_bound)); return absl::OkStatus(); }); absl::Status Literal::MoveFrom(Literal&& src_literal, const ShapeIndex& dest_shape_index) { const Shape& dest_subshape = ShapeUtil::GetSubshape(shape(), dest_shape_index); if (!ShapeUtil::Equal(dest_subshape, src_literal.shape())) { return InvalidArgument( "Destination subshape not equal to source shape: %s vs %s", ShapeUtil::HumanString(dest_subshape), ShapeUtil::HumanString(src_literal.shape())); } src_literal.root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); src_literal.shape_ = MaybeOwningShapePtr(&NilShape()); src_literal.root_piece_ = Piece(); src_literal.root_piece_.set_subshape(src_literal.shape_.get()); return absl::OkStatus(); } [&](const ShapeIndex& src_index, Piece* src_piece) { if (!src_piece->subshape().IsArray()) { return; } ShapeIndex dest_index = dest_shape_index; for (int64_t i : src_index) { dest_index.push_back(i); } Piece& dest_piece = piece(dest_index); dest_piece.DeallocateBuffers(); dest_piece.MoveDataFrom(*src_piece); }); absl::Status MutableLiteralBase::CopySliceFrom( const LiteralSlice& src_literal, absl::Span<const int64_t> src_base, absl::Span<const int64_t> dest_base, absl::Span<const int64_t> copy_size) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << shape(); TF_RET_CHECK(LayoutUtil::IsDenseArray(src_literal.shape())) << src_literal.shape(); TF_RET_CHECK(ShapeUtil::SameElementType(src_literal.shape(), shape())); TF_RET_CHECK(src_literal.shape().rank() == src_base.size()); TF_RET_CHECK(shape().rank() == dest_base.size()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, [&](auto primitive_type_constant) -> absl::Status { using NativeT = NativeTypeOf<primitive_type_constant>; return CopySliceFromInternal<NativeT>(src_literal, src_base, dest_base, copy_size); }, void MutableLiteralBase::PopulateR1(const tsl::core::Bitmap& values) { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(element_count(), values.bits()); CHECK_EQ(shape().element_type(), PRED); for (int64_t i = 0; i < static_cast<int64_t>(values.bits()); ++i) { Set({i}, values.get(i)); } } void MutableLiteralBase::PopulateInplaceInternal( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator, bool parallel) { const Shape& this_shape = shape(); const int64_t rank = this_shape.rank(); DCHECK(LayoutUtil::IsDenseArray(this_shape)); char* const dest_base = static_cast<char*>(untyped_data()); if (rank > 0) { StrideConfig stride_config(this_shape, this_shape, this_shape.dimensions()); const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); const int64_t num_elements = ShapeUtil::ElementsIn(shape()); // If we are rank-1 and we are `parallel`, it is better to use a smaller // `step` than what `StrideConfig` does: stick the entire dimension in the // inner-most loop. if (parallel && this_shape.rank() == 1) { const int64_t thread_count = ShapeUtil::GetForEachIndexParallelThreadCount(); // Let's just divide up the array into small amounts per thread. stride_config.dest_stride = stride_config.minor_loop_size = num_elements > 32 ? std::max<int64_t>(num_elements / thread_count, 1) : num_elements; stride_config.step = {stride_config.minor_loop_size}; } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; if (parallel) { ShapeUtil::ForEachIndexParallel(this_shape, stride_config.base, stride_config.dimensions, stride_config.step, init_function); } else { ShapeUtil::ForEachIndex( this_shape, stride_config.base, stride_config.dimensions, stride_config.step, [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); } } else { // For scalars. populator(dest_base, {}, /*thread_id=*/-1); } } auto init_function = [&](absl::Span<const int64_t> indexes, int thread_id) -> absl::StatusOr<bool> { const int64_t index = IndexUtil::MultidimensionalIndexToLinearIndex(shape(), indexes); DimensionVector minor_scan_indexes(rank, 0); std::copy(indexes.begin(), indexes.end(), minor_scan_indexes.begin()); char* dest_ptr = dest_base + index * primitive_size; char* const dest_end = dest_base + // This handles the case where minor_loop_size does not evenly divide // the most minor dimension. std::min(index + stride_config.minor_loop_size, num_elements) * primitive_size; while (dest_ptr < dest_end) { populator(dest_ptr, minor_scan_indexes, thread_id); ++minor_scan_indexes[stride_config.minor_dimension]; dest_ptr += primitive_size; } return true; }; [&init_function]( absl::Span<const int64_t> indexes) -> absl::StatusOr<bool> { auto result_ignored = init_function(indexes, /*thread_id=*/-1); return true; }); absl::Status MutableLiteralBase::PopulateInplace( absl::FunctionRef<void(void*, absl::Span<const int64_t>)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal( [&](void* dest, absl::Span<const int64_t> indexes, int /*thread_id*/) { return populator(dest, indexes); }, /*parallel=*/false); return absl::OkStatus(); } absl::Status MutableLiteralBase::PopulateInplaceParallel( absl::FunctionRef<void(void*, absl::Span<const int64_t>, int)> populator) { TF_RET_CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); PopulateInplaceInternal(populator, /*parallel=*/element_count() > 32); return absl::OkStatus(); } Literal LiteralBase::Relayout(const Layout& new_layout, const ShapeIndex& shape_index) const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); Shape* subshape = ShapeUtil::GetMutableSubshape(&new_shape, shape_index); TF_CHECK_OK(LayoutUtil::ValidateLayoutForShape(new_layout, *subshape)); *subshape->mutable_layout() = new_layout; // LINT.IfChange // s4 literals are stored in uint8_t/int8_t, therefore element_size_in_bits // must be removed. if (subshape->layout().element_size_in_bits() == 4) { subshape->mutable_layout()->set_element_size_in_bits(0); } // LINT.ThenChange(//tensorflow/compiler/xla/types.h) Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this)); return result; } Literal LiteralBase::Relayout(const Shape& shape_with_layout) const { CHECK(ShapeUtil::Compatible(shape_with_layout, shape())) << "Given shape_with_layout " << ShapeUtil::HumanString(shape_with_layout) << " not compatible with literal shape " << ShapeUtil::HumanString(shape()); Literal result = CreateFromShape(shape_with_layout); ShapeUtil::ForEachSubshape( result.shape(), [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); return result; } [this, &result](const Shape& subshape, const ShapeIndex& index) { if (subshape.IsArray()) { TF_CHECK_OK(result.CopyFrom(*this, /*dest_shape_index=*/index, /*src_shape_index=*/index)); } }); Literal LiteralBase::ToBoundedDynamic(const Shape& bounded_shape) const { CHECK(bounded_shape.is_dynamic()); Literal result(bounded_shape); ShapeUtil::ForEachSubshape( shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } shape(), [&](const Shape& subshape, const ShapeIndex& index) { if (!subshape.IsArray()) { return; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (bounded_shape.is_dynamic_dimension(i)) { result.SetDynamicSize(i, subshape.dimensions(i)); } } }); Literal LiteralBase::ToStatic() const { // Create new shape with 'new_layout' set at the given shape index. Shape new_shape = shape(); ShapeUtil::ForEachMutableSubshape( &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); Literal result(new_shape); TF_CHECK_OK(result.CopyFrom(*this, {}, {}, /*only_dynamic_bound=*/true)); return result; } &new_shape, [this](Shape* subshape, const ShapeIndex& index) { if (!subshape->IsArray()) { return; } for (int64_t i = 0; i < subshape->rank(); ++i) { // GetDynamicSize has a 32-bit return type and may truncate static // dimensions, so make sure to skip. if (!subshape->is_dynamic_dimension(i)) continue; subshape->set_dynamic_dimension(i, false); subshape->set_dimensions(i, GetDynamicSize(i, index)); } }); absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } absl::StatusOr<Literal> BroadcastHelper(const LiteralBase& src, const Shape& src_shape, const Shape& result_shape, absl::Span<const int64_t> dimensions) { for (int64_t i = 0, end = dimensions.size(); i < end; i++) { TF_RET_CHECK(src_shape.dimensions(i) == result_shape.dimensions(dimensions[i])); } TF_RET_CHECK(result_shape.element_type() == src_shape.element_type()); Literal result(result_shape); if (src_shape.is_dynamic()) { for (int64_t i = 0; i < dimensions.size(); ++i) { if (src_shape.is_dynamic_dimension(i)) { // Set any dynamic sizes in the new literal. int64_t dynamic_size = src.GetDynamicSize(i); result.SetDynamicSize(dimensions[i], dynamic_size); } } } // scratch_source_index is temporary storage space for the computed index into // the input literal. We put it here to avoid allocating an std::vector in // every iteration of ShapeUtil::ForEachIndex. int src_shape_dims = src_shape.dimensions_size(); std::vector<int64_t> scratch_source_index(src_shape_dims); // Make the span once outside the ForEachIndex... loop, pointing into // scratch_source_index absl::Span<int64_t> scratch_source_span(scratch_source_index); int64_t* scratch_source_array = scratch_source_span.data(); const char* source_data = static_cast<const char*>(src.untyped_data()); char* dest_data = static_cast<char*>(result.untyped_data()); auto src_minor_to_major = LayoutUtil::MinorToMajor(src_shape); auto result_minor_to_major = LayoutUtil::MinorToMajor(result_shape); ShapeUtil::ForEachIndexNoStatus( result_shape, [&](absl::Span<const int64_t> output_index) { // Compute dest_index int64_t dest_index = IndexUtil::MultidimensionalIndexToLinearIndex( result_shape, result_minor_to_major, output_index); // Compute source_index int64_t source_index; for (int64_t i = 0, end = dimensions.size(); i < end; ++i) { scratch_source_array[i] = output_index[dimensions[i]]; } if (src_shape_dims == 1) { // Fast path for this case source_index = scratch_source_array[0]; DCHECK_EQ(source_index, IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span)); } else { source_index = IndexUtil::MultidimensionalIndexToLinearIndex( src_shape, src_minor_to_major, scratch_source_span); } // Move one element from source_index in source to dest_index in dest memcpy(dest_data + PRIMITIVE_SIZE * dest_index, source_data + PRIMITIVE_SIZE * source_index, PRIMITIVE_SIZE); return true; }); return std::move(result); } result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { result_shape, [&](absl::Span<const int64_t> output_index) { absl::StatusOr<Literal> LiteralBase::Broadcast( const Shape& result_shape, absl::Span<const int64_t> dimensions) const { const LiteralBase& src = *this; const Shape& src_shape = shape(); if (!src_shape.IsArray()) { return InvalidArgument("Broadcast only supports arrays."); } const int64_t primitive_size = ShapeUtil::ByteSizeOfPrimitiveType(src_shape.element_type()); switch (primitive_size) { case 0: return BroadcastHelper<0>(src, src_shape, result_shape, dimensions); case 1: return BroadcastHelper<1>(src, src_shape, result_shape, dimensions); case 2: return BroadcastHelper<2>(src, src_shape, result_shape, dimensions); case 4: return BroadcastHelper<4>(src, src_shape, result_shape, dimensions); case 8: return BroadcastHelper<8>(src, src_shape, result_shape, dimensions); case 16: return BroadcastHelper<16>(src, src_shape, result_shape, dimensions); default: LOG(FATAL) << "Unhandled primitive size " << primitive_size; return InvalidArgument("Unhandled primitive size"); break; } } absl::StatusOr<Literal> LiteralBase::Reshape( absl::Span<const int64_t> dimensions) const { if (!LayoutUtil::IsDenseArray(shape())) { return InvalidArgument("Reshape is only supported for dense arrays."); } if (shape().is_dynamic()) { // TODO(b/243182930): We should consider supporting dynamic reshape. return Unimplemented("Dynamic reshape is not implemented."); } Literal output; if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); } else { output = Clone(); } // Because the layout is monotonic, we can simply reuse the same sequence of // values without changing their order. *output.mutable_shape_do_not_use() = ShapeUtil::MakeShape(shape().element_type(), dimensions); int64_t elements_before = ShapeUtil::ElementsIn(shape()); int64_t elements_after = ShapeUtil::ElementsIn(output.shape()); if (elements_before != elements_after) { return InvalidArgument( "Shapes before and after Literal::Reshape have different numbers " "of elements: %s vs %s.", ShapeUtil::HumanString(shape()), ShapeUtil::HumanString(output.shape())); } return std::move(output); } Literal LiteralBase::Transpose(absl::Span<const int64_t> permutation) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); CHECK(shape().rank() == permutation.size() && IsPermutation(permutation)) << "Given permutation is not a permutation of dimension numbers"; // To transpose the array, we just permute the dimensions and layout, and // do a straight memory copy of the raw data set. // This is considerably faster than iterating over every array element using // the EachCell<>() and Set<>() APIs. Shape permuted_shape = ShapeUtil::PermuteDimensions(permutation, shape()); // Replace the layout with one affine to this shape, such that a // transpose operation can be performed by leaving the flat values // representation intact. // For example, consider the shape F32[11,8]{1,0} under a {1,0} permutation. // The shape with affine layout resulting from that operation will be // F32[8,11]{0,1}, since it leaves the original most minor (the 8 sized), the // most minor. // // Essentially, given MinMaj(Di) the position of the Di dimension within the // minor to major vector, and given T(Di) the index that the original Di // dimension has within the transposed array, a layout is affine if // MinMaj(Di) == TMinMaj(T(Di)), with TMinMaj() being the minor to major // vector of the affine layout. std::vector<int64_t> inverse_permutation = InversePermutation(permutation); CHECK(LayoutUtil::IsDenseArray(permuted_shape)); Layout* layout = permuted_shape.mutable_layout(); layout->clear_minor_to_major(); for (auto index : LayoutUtil::MinorToMajor(shape())) { layout->add_minor_to_major(inverse_permutation[index]); } Literal new_literal(permuted_shape); if (shape().is_dynamic()) { for (int64_t i = 0; i < shape().rank(); i++) { if (shape().is_dynamic_dimension(i)) { // Set the dynamic size of any dynamic dimension in the transposed // literal. new_literal.SetDynamicSize(inverse_permutation[i], GetDynamicSize(i)); } } } DCHECK_EQ(ShapeUtil::ByteSizeOf(new_literal.shape()), ShapeUtil::ByteSizeOf(shape())); std::memcpy(new_literal.untyped_data(), untyped_data(), size_bytes()); return new_literal; } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } void SliceInternal(const LiteralBase& src_literal, absl::Span<const int64_t> start_indices, Literal& result_literal) { const Shape& result_shape = result_literal.shape(); DimensionVector new_indices(result_shape.rank()); TF_CHECK_OK( result_literal.Populate<NativeT>([&](absl::Span<const int64_t> indices) { for (int64_t i = 0; i < result_shape.rank(); ++i) { new_indices[i] = indices[i] + start_indices[i]; } return src_literal.Get<NativeT>(new_indices); })); for (int64_t dnum = 0; dnum < src_literal.shape().rank(); ++dnum) { if (src_literal.shape().is_dynamic_dimension(dnum)) { int64_t dynamic_size = src_literal.GetDynamicSize(dnum) - start_indices[dnum]; CHECK_GE(dynamic_size, 0) << src_literal.GetDynamicSize(dnum); dynamic_size = std::min(dynamic_size, result_shape.dimensions(dnum)); result_literal.SetDynamicSize(dnum, dynamic_size); } } } TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( TF_CHECK_OK( Literal LiteralBase::Slice(absl::Span<const int64_t> start_indices, absl::Span<const int64_t> limit_indices) const { CHECK(shape().IsArray()) << "tuple is not supported for slice"; DimensionVector result_dimensions; for (int64_t dnum = 0; dnum < shape().rank(); ++dnum) { CHECK_GE(start_indices[dnum], 0); CHECK_LE(limit_indices[dnum], shape().dimensions(dnum)) << "dnum = " << dnum; int64_t dimension = limit_indices[dnum] - start_indices[dnum]; CHECK_GE(dimension, 0) << "dnum = " << dnum; result_dimensions.push_back(dimension); } auto result_shape = ShapeUtil::MakeShapeWithDenseLayout( shape().element_type(), result_dimensions, LayoutUtil::MinorToMajor(shape())); ShapeUtil::CopyDynamicDimensions(&result_shape, shape()); Literal result_literal(result_shape); primitive_util::ArrayTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; return SliceInternal<NativeT>(*this, start_indices, result_literal); }, result_shape.element_type()); return result_literal; } Literal LiteralBase::Clone() const { Literal result(shape()); TF_CHECK_OK(result.CopyFrom(*this)); return result; } std::unique_ptr<Literal> LiteralBase::CloneToUnique() const { auto result = std::make_unique<Literal>(shape()); TF_CHECK_OK(result->CopyFrom(*this)); return result; } bool LiteralBase::IsDetermined(const ShapeIndex& shape_index) const { return piece(shape_index).IsDetermined(); } bool LiteralBase::IsKnown(const ShapeIndex& shape_index) const { return piece(shape_index).IsKnown(); } std::string LiteralBase::GetAsString(absl::Span<const int64_t> multi_index, const ShapeIndex& shape_index) const { const Shape& subshape = ShapeUtil::GetSubshape(shape(), shape_index); CHECK(LayoutUtil::IsDenseArray(subshape)); return primitive_util::ArrayTypeSwitch<std::string>( [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, subshape.element_type()); } [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, [&](auto primitive_type_constant) -> std::string { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsIntegralType(primitive_type_constant)) { return StrCat(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return RoundTripFpToString(Get<NativeT>(multi_index, shape_index)); } if constexpr (primitive_util::IsComplexType(primitive_type_constant)) { NativeT c = Get<NativeT>(multi_index, shape_index); return StrCat("(", RoundTripFpToString(c.real()), ", ", RoundTripFpToString(c.imag()), ")"); } if constexpr (primitive_type_constant == PRED) { return Get<bool>(multi_index, shape_index) ? "true" : "false"; } LOG(FATAL) << PrimitiveType_Name(subshape.element_type()); }, std::optional<int64_t> LiteralBase::GetIntegralAsS64( absl::Span<const int64_t> multi_index) const { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(multi_index); } return std::nullopt; }, std::optional<double> LiteralBase::GetAsDouble( absl::Span<const int64_t> multi_index) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); return primitive_util::PrimitiveTypeSwitch<std::optional<double>>( [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, s.element_type()); } [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<double> { if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; return static_cast<double>(Get<NativeT>(multi_index)); } return std::nullopt; }, std::optional<double> LiteralBase::GetSumAsDouble( absl::Span<const int64_t> linear_indices) const { const Shape& s = shape(); CHECK(LayoutUtil::IsDenseArray(s)); if (!primitive_util::IsFloatingPointType(s.element_type())) { return std::nullopt; } return primitive_util::FloatingPointTypeSwitch<double>( [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, s.element_type()); } [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, [&](auto primitive_type_constant) -> double { using NativeT = NativeTypeOf<primitive_type_constant>; double sum = 0.0; auto d = root_piece().data<NativeT>(); for (const int64_t idx : linear_indices) { sum += static_cast<double>(d[idx]); } return sum; }, std::optional<complex128> LiteralBase::GetAsComplex128( absl::Span<const int64_t> multi_index) const { return primitive_util::PrimitiveTypeSwitch<std::optional<complex128>>( [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, [&](auto primitive_type_constant) -> std::optional<complex128> { if constexpr (primitive_util::IsArrayType(primitive_type_constant)) { using NativeT = NativeTypeOf<primitive_type_constant>; if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { return {Get<NativeT>(multi_index)}; } if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } if constexpr (primitive_util::IsIntegralType( primitive_type_constant) && primitive_type_constant != S64 && primitive_type_constant != U64) { return {{static_cast<double>(Get<NativeT>(multi_index)), 0}}; } } return std::nullopt; }, absl::Status MutableLiteralBase::SetIntegralAsS64( absl::Span<const int64_t> multi_index, int64_t value) { CHECK(LayoutUtil::IsDenseArray(shape())); return primitive_util::PrimitiveTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsIntegralType(primitive_type_constant) || primitive_type_constant == PRED) { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); return absl::OkStatus(); } return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); }, absl::Status MutableLiteralBase::SetFromDouble( absl::Span<const int64_t> multi_index, double value) { CHECK(LayoutUtil::IsDenseArray(shape())); if (!primitive_util::IsFloatingPointType(shape().element_type())) { return FailedPrecondition("Array element type is not integral: %s", PrimitiveType_Name(shape().element_type())); } primitive_util::FloatingPointTypeSwitch<void>( [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, shape().element_type()); return absl::OkStatus(); } [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, [&](auto primitive_type_constant) -> void { using NativeT = NativeTypeOf<primitive_type_constant>; Set<NativeT>(multi_index, static_cast<NativeT>(value)); }, void PrintShape(bool print_layout, const Shape& shape, Printer* printer) { if (print_layout) { ShapeUtil::PrintHumanStringWithLayout(printer, shape); } else { ShapeUtil::PrintHumanString(printer, shape); } } void TuplePrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); printer->Append(oneline ? "( " : "(\n"); for (int i = 0; i < ShapeUtil::TupleElementCount(subshape); ++i) { ShapeIndex element_index = shape_index; element_index.push_back(i); if (i > 0) printer->Append(oneline ? ", " : ",\n"); PrintHelper(literal, element_index, print_shape, print_layout, oneline, printer); } printer->Append(oneline ? " )" : "\n)"); } void DenseArrayPrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); int64_t rank = subshape.rank(); const absl::string_view linebreak = oneline ? " " : "\n"; std::function<void(absl::Span<const int64_t> dimensions, std::vector<int64_t>*)> print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; if (print_shape) { PrintShape(print_layout, subshape, printer); if (subshape.is_dynamic()) { printer->Append("("); for (int64_t i = 0; i < subshape.dimensions_size(); ++i) { printer->Append(literal.GetDynamicSize(i, shape_index)); if (i < subshape.dimensions_size() - 1) { printer->Append(","); } } printer->Append(")"); } printer->Append(" "); } std::vector<int64_t> indices = {}; std::vector<int64_t> dimensions; dimensions.reserve(subshape.rank()); for (int64_t i = 0; i < subshape.rank(); ++i) { dimensions.push_back(literal.GetDynamicSize(i, shape_index)); } print_recursive(dimensions, &indices); } print_recursive = [&](absl::Span<const int64_t> dimensions, std::vector<int64_t>* accum_indices) { // dimensions.size() decreases by 1 at each recursive call, // and accum_indices->size() increases by 1. // Their sum is equal to the rank of the tensor. CHECK_EQ(rank, dimensions.size() + accum_indices->size()); auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; if (dimensions.empty()) { // Display predicates as 0s and 1s so that the string is more dense. std::string elem; if (subshape.element_type() == PRED && rank > 0) { elem = literal.Get<bool>(*accum_indices, shape_index) ? "1" : "0"; } else { elem = literal.GetAsString(*accum_indices, shape_index); } printer->Append(elem); } else { printer->Append(brace_to_string("{")); for (int i = 0; i < dimensions[0]; ++i) { accum_indices->push_back(i); print_recursive(dimensions.subspan(1), accum_indices); accum_indices->pop_back(); if (i < dimensions[0] - 1) { printer->Append(","); printer->Append(dimensions.size() > 1 ? linebreak : " "); } } printer->Append(brace_to_string("}")); } }; auto brace_to_string = [&](std::string brace) -> std::string { // Handle 1D tensor if (rank == 1) { return brace; } // Handle the innermost tensor of a 2D+ tensor. if (dimensions.size() == 1 && brace == "{") { return StrCat(oneline ? "" : " ", brace, dimensions[0] <= 1 ? "" : " "); } if (dimensions.size() == 1 && brace == "}") { return StrCat(dimensions[0] <= 1 ? "" : " ", brace); } // Handle the non-innermost tensors of a 2D+ tensor. if (brace == "{") { const int64_t accum_indices_size = accum_indices->size(); if (rank > 3 && !accum_indices->empty() && accum_indices_size < rank) { int index = accum_indices->size() - 1; int value = accum_indices->back(); int size = dimensions.front(); return StrCat(brace, " /*i", index, "=", value, "*/", size > 0 ? linebreak : ""); } return StrCat(brace, linebreak); } return StrCat(linebreak, brace); }; void PrintHelper(const LiteralBase& literal, const ShapeIndex& shape_index, bool print_shape, bool print_layout, bool oneline, Printer* printer) { const Shape& subshape = ShapeUtil::GetSubshape(literal.shape(), shape_index); CHECK(LayoutUtil::HasLayout(literal.shape())); CHECK(LayoutUtil::HasLayout(subshape)); if (subshape.IsTuple()) { TuplePrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else if (subshape.IsToken()) { printer->Append("token"); } else { CHECK(LayoutUtil::IsDenseArray(subshape)); if (literal.IsKnown(shape_index)) { DenseArrayPrintHelper(literal, shape_index, print_shape, print_layout, oneline, printer); } else { PrintShape(print_layout, subshape, printer); printer->Append(" "); if (literal.IsDetermined(shape_index)) { printer->Append("unknown"); } else { printer->Append("undetermined"); } } } } void LiteralBase::Print(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithoutShape(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/false, printer); } void LiteralBase::PrintWithoutShapeOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/false, /*print_layout=*/false, /*oneline=*/true, printer); } void LiteralBase::PrintWithLayout(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/false, printer); } void LiteralBase::PrintWithLayoutOneline(Printer* printer) const { CHECK(LayoutUtil::HasLayout(this->shape())); PrintHelper(*this, {}, /*print_shape=*/true, /*print_layout=*/true, /*oneline=*/true, printer); } std::string LiteralBase::ToString() const { StringPrinter printer; Print(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringOneline() const { StringPrinter printer; PrintOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShape() const { StringPrinter printer; PrintWithoutShape(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithoutShapeOneline() const { StringPrinter printer; PrintWithoutShapeOneline(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayout() const { StringPrinter printer; PrintWithLayout(&printer); return std::move(printer).ToString(); } std::string LiteralBase::ToStringWithLayoutOneline() const { StringPrinter printer; PrintWithLayoutOneline(&printer); return std::move(printer).ToString(); } void LiteralBase::EachCellAsString( absl::FunctionRef<void(absl::Span<const int64_t> indices, const std::string& value)> per_cell) const { if (ShapeUtil::IsZeroElementArray(shape())) { return; } auto indices = IndexUtil::LinearIndexToMultidimensionalIndex( shape(), /*linear_index=*/0); do { per_cell(indices, GetAsString(indices)); } while (IndexUtil::BumpIndices(shape(), absl::MakeSpan(indices))); } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } void ConvertBetweenNativeTypes(absl::Span<const NativeSrcT> src_data, void* dst_base) { static_assert(!std::is_same_v<NativeSrcT, NativeDestT>); auto converter = [](NativeSrcT src) -> NativeDestT { // C++ [conv.bool]p1: // A prvalue of arithmetic [...] type can be converted to a prvalue of // type bool. A zero value [...] is converted to false; any other value is // converted to true. // C++ [conv.fpint]p1: // [...] The behavior is undefined if the truncated value cannot be // represented in the destination type. // // Using static_cast to convert a float to an integral type other than bool // may be undefined if the value's magnitude is too large or it is a NaN. // Let's choose saturating arithmetic as it captures the spirit of infinity // and arbitrarily map NaN to zero. if constexpr (!std::is_same_v<NativeDestT, bool> && !std::numeric_limits<NativeSrcT>::is_integer && std::numeric_limits<NativeDestT>::is_integer) { if (src != src) { return NativeDestT{0}; } if (src >= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::max())) { return std::numeric_limits<NativeDestT>::max(); } if (src <= static_cast<NativeSrcT>(std::numeric_limits<NativeDestT>::lowest())) { return std::numeric_limits<NativeDestT>::lowest(); } } return static_cast<NativeDestT>(src); }; NativeDestT* dest_data = static_cast<NativeDestT*>(dst_base); for (const NativeSrcT& src : src_data) { *(dest_data++) = converter(src); } } auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { auto converter = [](NativeSrcT src) -> NativeDestT { absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } absl::Status ConvertIfDestTypeMatches(const LiteralBase& src_literal, MutableLiteralBase& dst_literal) { DCHECK(dst_literal.shape().IsArray()); using NativeSrcT = NativeTypeOf<kSrcType>; // Pass raw data Span/pointers to called template methods to avoid duplicating // the Literal method calls to many time which hurts code size. auto src_data = src_literal.data<NativeSrcT>(); void* dst_base = dst_literal.untyped_data(); DCHECK_EQ(src_data.size(), dst_literal.element_count()); return primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { if constexpr (primitive_util::IsComplexType(kSrcType) && !primitive_util::IsComplexType(primitive_type_constant)) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(kSrcType), PrimitiveType_Name(primitive_type_constant())); } else if constexpr (kSrcType != primitive_type_constant) { using NativeDestT = NativeTypeOf<primitive_type_constant>; ConvertBetweenNativeTypes<NativeSrcT, NativeDestT>(src_data, dst_base); } return absl::OkStatus(); }, dst_literal.shape().element_type()); } [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { [&](auto primitive_type_constant) -> absl::Status { absl::StatusOr<Literal> ConvertSwitch(const LiteralBase& literal, PrimitiveType primitive_dest_type) { TF_RET_CHECK(LayoutUtil::IsDenseArray(literal.shape())); if (literal.shape().element_type() == primitive_dest_type) { return literal.Clone(); } // Source Array type requirement is ensured by IsDenseArray before. if (!primitive_util::IsArrayType(primitive_dest_type) || !primitive_util::IsArrayType(literal.shape().element_type())) { return Unimplemented("%s from type %s to type %s is not implemented.", "Converting", PrimitiveType_Name(literal.shape().element_type()), PrimitiveType_Name(primitive_dest_type)); } // At this point, we know both src & dst are array types, while src is not // complex type, so we can allocate the result literal here to avoid // duplicating it N^2 times in the conversion implementation. Literal result( ShapeUtil::ChangeElementType(literal.shape(), primitive_dest_type)); TF_RETURN_IF_ERROR(primitive_util::ArrayTypeSwitch<absl::Status>( [&](auto primitive_type_constant) -> absl::Status { return ConvertIfDestTypeMatches<primitive_type_constant>(literal, result); }, literal.shape().element_type())); return result; } absl::StatusOr<Literal> LiteralBase::Convert( PrimitiveType primitive_dest_type) const { return ConvertSwitch(*this, primitive_dest_type); } absl::StatusOr<Literal> LiteralBase::BitcastConvert( const Shape& dest_shape) const { if (ShapeUtil::ByteSizeOf(dest_shape) != ShapeUtil::ByteSizeOf(shape())) { return InvalidArgument( "Can not bitcast-convert from shape %s to a shape of different size %s", shape().ToString(), dest_shape.ToString()); } if (dest_shape.IsTuple() || shape().IsTuple()) { return InvalidArgument( "bitcast-convert is not valid for tuple shapes %s->%s", shape().ToString(), dest_shape.ToString()); } if (shape().is_dynamic() || dest_shape.is_dynamic()) { return InvalidArgument( "bitcast-convert is not valid for dynamic shape %s->%s", shape().ToString(), dest_shape.ToString()); } Literal out(dest_shape); std::memcpy(out.root_piece_.buffer(), root_piece().buffer(), root_piece().size_bytes_dense()); // Perform the reshape on little endian encoding even on big endian machines. if constexpr (!kLittleEndian) { // Swap byte ordering as per the input data type. size_t input_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(shape().element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), input_elem_size, out.root_piece().size_bytes_dense() / input_elem_size)); // Swap byte ordering as per the output data type. size_t output_elem_size = ShapeUtil::ByteSizeOfPrimitiveType(dest_shape.element_type()); TF_RETURN_IF_ERROR(tsl::ByteSwapArray( const_cast<char*>(out.root_piece().buffer()), output_elem_size, out.root_piece().size_bytes_dense() / output_elem_size)); } return out; } absl::StatusOr<Literal> LiteralBase::ConvertToShape( const Shape& dest_shape) const { if (!dest_shape.IsTuple()) { return Convert(dest_shape.element_type()); } std::vector<Literal> elements; const auto tuple_element_count = ShapeUtil::TupleElementCount(shape()); elements.reserve(tuple_element_count); for (int i = 0; i < tuple_element_count; ++i) { auto element = LiteralSlice(*this, {i}); TF_ASSIGN_OR_RETURN( auto new_element, element.ConvertToShape(ShapeUtil::GetSubshape(dest_shape, {i}))); elements.push_back(std::move(new_element)); } return MutableLiteralBase::MoveIntoTuple(absl::MakeSpan(elements)); } /* static */ Literal MutableLiteralBase::MoveIntoTuple( absl::Span<Literal> elements) { std::vector<const Shape*> element_shapes; element_shapes.reserve(elements.size()); for (const Literal& element : elements) { element_shapes.push_back(&element.shape()); } Literal literal(ShapeUtil::MakeTupleShapeWithPtrs(element_shapes), /*allocate_arrays=*/false); for (int i = 0, end = elements.size(); i < end; ++i) { TF_CHECK_OK( literal.MoveFrom(std::move(elements[i]), /*dest_shape_index=*/{i})); } return literal; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualElementsInternal( const LiteralBase::Piece& other, std::vector<int64_t>* multi_index) const { if (multi_index->size() == subshape().rank()) { return (Get<NativeT>(*multi_index) == other.Get<NativeT>(*multi_index)); } for (int64_t i = 0; i < GetDynamicSize(multi_index->size()); ++i) { multi_index->push_back(i); if (!EqualElementsInternal<NativeT>(other, multi_index)) { return false; } multi_index->pop_back(); } return true; } bool LiteralBase::Piece::EqualDynamicSize( const LiteralBase::Piece& other) const { DCHECK(ShapeUtil::Compatible(subshape(), other.subshape())); if (subshape().is_static()) { return true; } for (int64_t i = 0; i < subshape().rank(); ++i) { if (GetDynamicSize(i) != other.GetDynamicSize(i)) { return false; } } return true; } bool LiteralBase::Piece::EqualElements(const LiteralBase::Piece& other) const { if (subshape().is_static() && ShapeUtil::Equal(subshape(), other.subshape()) && subshape().IsArray()) { CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(size_bytes_dense(), other.size_bytes_dense()); if (primitive_util::IsSubByteNonPredType(subshape().element_type())) { CHECK(!primitive_util::IsFloatingPointType(subshape().element_type())); auto one_array = buffer(); auto two_array = other.buffer(); const int bits_per_element = primitive_util::BitWidth(subshape().element_type()); const uint8_t mask = LsbMask<uint8_t>(bits_per_element); for (int64_t i = 0; i < size_bytes_dense(); ++i) { if ((one_array[i] & mask) != (two_array[i] & mask)) return false; } return true; } return memcmp(buffer(), other.buffer(), size_bytes_dense()) == 0; } std::vector<int64_t> multi_index; return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeSrcT = NativeTypeOf<primitive_type_constant>; return EqualElementsInternal<NativeSrcT>(other, &multi_index); }, subshape().element_type()); } bool LiteralBase::Equal(const LiteralBase& other, bool layout_sensitive) const { // Checking the structure of tuple literals. Checks for dense arrays are // performed below. if (!ShapeUtil::EqualStructure(shape(), other.shape())) { return false; } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); } return root_piece().ForEachSubpieceWithBool([&](const ShapeIndex& index, const Piece& piece) { const Piece& other_piece = other.piece(index); const Shape& subshape = piece.subshape(); const Shape& other_subshape = other_piece.subshape(); if (subshape.element_type() != other_subshape.element_type()) { return false; } if (!piece.subshape().IsArray()) { return true; } if (subshape.rank() != other_subshape.rank()) { return false; } if (layout_sensitive && (subshape.layout() != other_subshape.layout())) { return false; } for (int64_t i = 0; i < subshape.rank(); ++i) { if (piece.GetDynamicSize(i) != other_piece.GetDynamicSize(i)) { return false; } } if (!piece.EqualElements(other_piece)) { return false; } return true; }); static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(NativeT a, NativeT b) { if constexpr (std::numeric_limits<NativeT>::has_quiet_NaN || std::numeric_limits<NativeT>::has_signaling_NaN) { if (Eigen::numext::isnan(a) && Eigen::numext::isnan(b)) { return true; } } return a == b; } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool EqualIncludingNan(std::complex<T> a, std::complex<T> b) { return EqualIncludingNan(a.real(), b.real()) && EqualIncludingNan(a.imag(), b.imag()); } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } static bool AllElementsEqualValue(absl::Span<const NativeT> data, NativeT value) { for (int64_t i = 0; i < data.size(); ++i) { if (!EqualIncludingNan(data[i], value)) { return false; } } return true; } bool Literal::Piece::IsAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, subshape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return AllElementsEqualValue(this->data<NativeT>(), scalar.GetFirstElement<NativeT>()); }, int64_t Literal::Piece::CountAll(const Literal& scalar) const { CHECK(ShapeUtil::IsScalar(scalar.shape())) << scalar.shape().ToString(); if (!subshape().IsArray()) { return 0; } CHECK(LayoutUtil::IsDenseArray(subshape())) << __func__ << " is only supported for dense arrays: " << subshape(); CHECK_EQ(subshape().element_type(), scalar.shape().element_type()); return primitive_util::ArrayTypeSwitch<int64_t>( [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, subshape().element_type()); } [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, [&](auto primitive_type_constant) -> int64_t { using NativeT = NativeTypeOf<primitive_type_constant>; return absl::c_count_if( this->data<NativeT>(), [&](NativeT elem) -> bool { return EqualIncludingNan(elem, scalar.GetFirstElement<NativeT>()); }); }, this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { this->data<NativeT>(), [&](NativeT elem) -> bool { bool LiteralBase::IsAll(const Literal& scalar) const { return root_piece().IsAll(scalar); } bool LiteralBase::IsAll(int8_t value) const { if (!shape().IsArray()) { return false; } PrimitiveType ty = shape().element_type(); if (primitive_util::IsFloatingPointType(ty)) { return IsAllFloatImpl(value, /*round_value=*/false); } if (primitive_util::IsUnsignedIntegralType(ty) && value < 0) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; NativeT converted(value); if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (!Eigen::numext::isfinite(converted)) { return false; } } if constexpr (!primitive_util::IsComplexType(primitive_type_constant)) { if (static_cast<int8_t>(converted) != value) { return false; } } scalar.Set<NativeT>({}, converted); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFloat(float value) const { return IsAllFloatImpl(value, /*round_value=*/true); } bool LiteralBase::IsAllFloatImpl(float value, bool round_value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsFloatingPointType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::FloatingPointTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); if (!round_value && scalar.GetAsDouble({}) != value) { return false; } return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllComplex(complex64 value) const { PrimitiveType ty = shape().element_type(); if (!primitive_util::IsComplexType(ty)) { return false; } Literal scalar(ShapeUtil::MakeScalarShape(ty)); return primitive_util::ComplexTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, ty); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; scalar.Set<NativeT>({}, static_cast<NativeT>(value)); return root_piece().IsAll(scalar); }, bool LiteralBase::IsAllFirst() const { if (!shape().IsArray()) { return false; } // Empty shapes are not all the first element since there is no first element. if (ShapeUtil::IsZeroElementArray(shape())) { return false; } absl::InlinedVector<int64_t, 4> start_indices(/*n=*/shape().rank(), 0); absl::InlinedVector<int64_t, 4> end_indices(/*n=*/shape().rank(), 1); Literal first = Slice(start_indices, end_indices); return IsAll(first.Reshape({}).value()); } bool LiteralBase::IsR1Iota() const { if (!shape().IsArray()) { return false; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); if (shape().rank() != 1) { return false; } return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; const int64_t elements = ShapeUtil::ElementsIn(shape()); for (int64_t idx = 0; idx < elements; ++idx) { if constexpr (primitive_util::IsIntegralType( primitive_type_constant)) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx) { return false; } } else if constexpr (primitive_util::IsFloatingPointType( primitive_type_constant)) { if (Get<NativeT>({idx}) != static_cast<NativeT>(idx)) { return false; } } else if constexpr (primitive_util::IsComplexType( primitive_type_constant)) { if (Get<NativeT>({idx}) != NativeT(idx, 0.0f)) { return false; } } else { // pred is not iota. return false; } } return true; }, std::optional<int64_t> LiteralBase::IsR1StridedIota() const { if (!shape().IsArray() || shape().rank() != 1) { return std::nullopt; } CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); const int64_t elements = ShapeUtil::ElementsIn(shape()); const PrimitiveType type = shape().element_type(); if (elements <= 1 || !primitive_util::IsIntegralType(type)) { return std::nullopt; } return primitive_util::IntegralTypeSwitch<std::optional<int64_t>>( [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, shape().element_type()); } [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, [&](auto primitive_type_constant) -> std::optional<int64_t> { using NativeT = NativeTypeOf<primitive_type_constant>; // Infer the stride as the second element (since first element is // supposed to be zero). const int64_t stride = static_cast<int64_t>(Get<NativeT>({1})); if (stride == 0) { return std::nullopt; } for (int64_t idx = 0; idx < elements; ++idx) { if (static_cast<int64_t>(Get<NativeT>({idx})) != idx * stride) { return std::nullopt; } } return stride; }, bool LiteralBase::IsZero(absl::Span<const int64_t> indices) const { CHECK(LayoutUtil::IsDenseArray(shape())) << __func__ << " is only supported for dense arrays: " << shape(); return primitive_util::ArrayTypeSwitch<bool>( [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, shape().element_type()); } [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, [&](auto primitive_type_constant) -> bool { using NativeT = NativeTypeOf<primitive_type_constant>; return Get<NativeT>(indices) == NativeT{0}; }, void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void CopyToRepeatedField(RepeatedFieldT* dest, const absl::Span<const NativeT> src) { *dest = RepeatedFieldT(src.begin(), src.end()); } void LiteralBase::Piece::set_array_value_state(ArrayValueState state) { array_value_state_ = state; } LiteralBase::ArrayValueState LiteralBase::Piece::get_array_value_state() const { return array_value_state_; } void LiteralBase::Piece::WriteToProto(LiteralProto* proto) const { *proto->mutable_shape() = subshape().ToProto(); switch (subshape().element_type()) { case PRED: CopyToRepeatedField(proto->mutable_preds(), data<bool>()); break; case U2: *proto->mutable_u2s() = std::string( reinterpret_cast<const char*>(data<u2>().data()), size_bytes_dense()); break; case U4: *proto->mutable_u4s() = std::string( reinterpret_cast<const char*>(data<u4>().data()), size_bytes_dense()); break; case U8: proto->set_u8s(static_cast<const unsigned char*>(data<uint8_t>().data()), element_count()); break; case U16: *proto->mutable_u16s() = std::string(reinterpret_cast<const char*>(data<uint16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_u16s()); } break; case U32: CopyToRepeatedField(proto->mutable_u32s(), data<uint32_t>()); break; case U64: CopyToRepeatedField(proto->mutable_u64s(), data<uint64_t>()); break; case S2: *proto->mutable_s2s() = std::string( reinterpret_cast<const char*>(data<s2>().data()), size_bytes_dense()); break; case S4: *proto->mutable_s4s() = std::string( reinterpret_cast<const char*>(data<s4>().data()), size_bytes_dense()); break; case S8: proto->set_s8s(static_cast<const signed char*>(data<int8_t>().data()), element_count()); break; case S16: *proto->mutable_s16s() = std::string(reinterpret_cast<const char*>(data<int16_t>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_s16s()); } break; case S32: CopyToRepeatedField(proto->mutable_s32s(), data<int32_t>()); break; case S64: CopyToRepeatedField(proto->mutable_s64s(), data<int64_t>()); break; case F8E5M2: *proto->mutable_f8e5m2s() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2>().data()), size_bytes_dense()); break; case F8E4M3FN: *proto->mutable_f8e4m3fns() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fn>().data()), size_bytes_dense()); break; case F8E4M3B11FNUZ: *proto->mutable_f8e4m3b11fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3b11fnuz>().data()), size_bytes_dense()); break; case F8E5M2FNUZ: *proto->mutable_f8e5m2fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e5m2fnuz>().data()), size_bytes_dense()); break; case F8E4M3FNUZ: *proto->mutable_f8e4m3fnuzs() = std::string( reinterpret_cast<const char*>(data<tsl::float8_e4m3fnuz>().data()), size_bytes_dense()); break; case F16: *proto->mutable_f16s() = std::string(reinterpret_cast<const char*>(data<half>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_f16s()); } break; case BF16: *proto->mutable_bf16s() = std::string(reinterpret_cast<const char*>(data<bfloat16>().data()), size_bytes_dense()); if (!kLittleEndian) { ConvertEndianShort(proto->mutable_bf16s()); } break; case F32: CopyToRepeatedField(proto->mutable_f32s(), data<float>()); break; case F64: CopyToRepeatedField(proto->mutable_f64s(), data<double>()); break; case C64: for (complex64 value : data<complex64>()) { proto->add_c64s(value.real()); proto->add_c64s(value.imag()); } break; case C128: for (complex128 value : data<complex128>()) { proto->add_c128s(value.real()); proto->add_c128s(value.imag()); } break; case TUPLE: case TOKEN: // Nothing to do but assign the shape which is done above. return; default: // TODO(b/111551621): Support serializing more PrimitiveTypes. LOG(FATAL) << "Unhandled primitive type " << PrimitiveType_Name(subshape().element_type()); } } const void* LiteralBase::Piece::untyped_data() const { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } void* LiteralBase::Piece::untyped_data() { DCHECK(LayoutUtil::IsDenseArray(subshape())) << ShapeUtil::HumanString(subshape()); return buffer(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status CopyFromRepeatedField(absl::Span<NativeT> dest, const RepeatedFieldT& src) { if (dest.size() != src.size()) { return InvalidArgument( "Expected %lu elements in LiteralProto repeated field, has %d", dest.size(), src.size()); } std::copy(src.begin(), src.end(), dest.begin()); return absl::OkStatus(); } absl::Status LiteralBase::Piece::CopyFromProto(const LiteralProto& proto) { // These conditions should have been checked in // MutableLiteralBase::CreateFromProto. TF_RET_CHECK(proto.has_shape()); Shape shape(proto.shape()); TF_RET_CHECK(LayoutUtil::HasLayout(shape)); TF_RET_CHECK(ShapeUtil::Equal(shape, subshape())); switch (subshape().element_type()) { case PRED: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<bool>(), proto.preds())); break; case S2: { const std::string& s(proto.s2s()); TF_RET_CHECK(data<s2>().size() * sizeof(s2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S4: { const std::string& s(proto.s4s()); TF_RET_CHECK(data<s4>().size() * sizeof(s4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case S8: { auto s8_data = data<int8_t>(); TF_RET_CHECK(proto.s8s().size() == s8_data.size()); std::copy(proto.s8s().begin(), proto.s8s().end(), s8_data.begin()); break; } case S16: { const std::string& s(proto.s16s()); TF_RET_CHECK(data<int16_t>().size() * sizeof(int16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case S32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int32_t>(), proto.s32s())); break; case S64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<int64_t>(), proto.s64s())); break; case U2: { const std::string& s(proto.u2s()); TF_RET_CHECK(data<u2>().size() * sizeof(u2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U4: { const std::string& s(proto.u4s()); TF_RET_CHECK(data<u4>().size() * sizeof(u4) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case U8: { auto u8_data = data<uint8_t>(); TF_RET_CHECK(proto.u8s().size() == u8_data.size()); std::copy(proto.u8s().begin(), proto.u8s().end(), u8_data.begin()); break; } case U16: { const std::string& s(proto.u16s()); TF_RET_CHECK(data<uint16_t>().size() * sizeof(uint16_t) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case U32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint32_t>(), proto.u32s())); break; case U64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<uint64_t>(), proto.u64s())); break; case F8E5M2: { const std::string& s(proto.f8e5m2s()); TF_RET_CHECK(data<tsl::float8_e5m2>().size() * sizeof(tsl::float8_e5m2) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FN: { const std::string& s(proto.f8e4m3fns()); TF_RET_CHECK(data<tsl::float8_e4m3fn>().size() * sizeof(tsl::float8_e4m3fn) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3B11FNUZ: { const std::string& s(proto.f8e4m3b11fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3b11fnuz>().size() * sizeof(tsl::float8_e4m3b11fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E5M2FNUZ: { const std::string& s(proto.f8e5m2fnuzs()); TF_RET_CHECK(data<tsl::float8_e5m2fnuz>().size() * sizeof(tsl::float8_e5m2fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F8E4M3FNUZ: { const std::string& s(proto.f8e4m3fnuzs()); TF_RET_CHECK(data<tsl::float8_e4m3fnuz>().size() * sizeof(tsl::float8_e4m3fnuz) == s.size()); memcpy(untyped_data(), s.data(), s.size()); break; } case F16: { const std::string& s(proto.f16s()); TF_RET_CHECK(data<half>().size() * sizeof(half) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case BF16: { const std::string& s(proto.bf16s()); TF_RET_CHECK(data<bfloat16>().size() * sizeof(bfloat16) == s.size()); memcpy(untyped_data(), s.data(), s.size()); if (!kLittleEndian) { ConvertEndianShort(reinterpret_cast<char*>(untyped_data()), s.size()); } break; } case F32: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<float>(), proto.f32s())); break; case F64: TF_RETURN_IF_ERROR(CopyFromRepeatedField(data<double>(), proto.f64s())); break; case C64: { auto complex_data = data<complex64>(); TF_RET_CHECK(proto.c64s_size() == complex_data.size() * 2); for (int64_t i = 0; i < complex_data.size(); ++i) { complex_data[i] = complex64{proto.c64s(i * 2), proto.c64s(i * 2 + 1)}; } break; } case C128: { auto complex_data = data<complex128>(); const int64_t complex_data_size_doubled = complex_data.size() * 2; TF_RET_CHECK(proto.c128s_size() == complex_data_size_doubled); for (int64_t i = 0, end = complex_data.size(); i < end; ++i) { complex_data[i] = complex128{proto.c128s(i * 2), proto.c128s(i * 2 + 1)}; } break; } case TUPLE: return InvalidArgument("Should not be called on tuple shapes: %s", ShapeUtil::HumanString(subshape())); default: return InvalidArgument("Is called on unsupported shape: %s", ShapeUtil::HumanString(subshape())); } return absl::OkStatus(); } bool LiteralBase::Piece::IsKnown() const { if (array_value_state_ != ArrayValueState::kKnown) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_known = true; ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); return are_all_leaf_arrays_known; } return true; } ForEachSubpiece([&are_all_leaf_arrays_known](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_known &= piece.IsKnown(); }); bool LiteralBase::Piece::IsDetermined() const { if (array_value_state_ == ArrayValueState::kUndetermined) { return false; } if (subshape().IsTuple()) { bool are_all_leaf_arrays_determined = true; ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); return are_all_leaf_arrays_determined; } return true; } ForEachSubpiece([&are_all_leaf_arrays_determined](const ShapeIndex& index, const Piece& piece) { if (!piece.subshape().IsArray()) { return; } are_all_leaf_arrays_determined &= piece.IsDetermined(); }); LiteralProto LiteralBase::ToProto() const { LiteralProto proto; root_piece().ForEachSubpiece( [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); return proto; } [&](const ShapeIndex& index, const Piece& piece) { LiteralProto* proto_piece = &proto; for (int64_t i : index) { while (proto_piece->tuple_literals_size() <= i) { proto_piece->add_tuple_literals(); } proto_piece = proto_piece->mutable_tuple_literals(i); } piece.WriteToProto(proto_piece); }); const void* LiteralBase::untyped_data(const ShapeIndex& shape_index) const { return piece(shape_index).untyped_data(); } void* MutableLiteralBase::untyped_data(const ShapeIndex& shape_index) { return piece(shape_index).untyped_data(); } int64_t LiteralBase::size_bytes(const ShapeIndex& shape_index) const { return piece(shape_index).size_bytes_dense(); } std::string LiteralBase::GetR1U8AsString() const { CHECK(shape().IsArray()); CHECK_EQ(shape().rank(), 1); CHECK_EQ(shape().element_type(), U8); return std::string(absl::bit_cast<const char*>(data<uint8_t>().data()), ShapeUtil::ElementsIn(shape())); } void MutableBorrowingLiteral::CopyPieceSubtree(const Shape& shape, const Piece* src_piece, Piece* dest_piece) { DCHECK(ShapeUtil::Equal(src_piece->subshape(), dest_piece->subshape())) << "src_piece has shape: " << ShapeUtil::HumanString(src_piece->subshape()) << "dest_piece has shape: " << ShapeUtil::HumanString(dest_piece->subshape()); dest_piece->set_array_value_state(src_piece->get_array_value_state()); if (shape.IsTuple()) { for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) { const Shape& subshape = shape.tuple_shapes(i); Piece child_piece; child_piece.set_subshape(&subshape); CopyPieceSubtree(subshape, &src_piece->child(i), &child_piece); dest_piece->emplace_back(std::move(child_piece)); } } else if (shape.IsArray()) { dest_piece->set_buffer(const_cast<char*>(src_piece->buffer())); } } MutableLiteralBase::~MutableLiteralBase() = default; MutableBorrowingLiteral::MutableBorrowingLiteral( const MutableBorrowingLiteral& literal) : MutableLiteralBase() { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); } MutableBorrowingLiteral& MutableBorrowingLiteral::operator=( const MutableBorrowingLiteral& literal) { shape_ = literal.shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.root_piece(), root_piece_); return *this; } MutableBorrowingLiteral::MutableBorrowingLiteral(MutableLiteralBase* literal) : MutableLiteralBase() { shape_ = literal->shape_.Clone(); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal->root_piece(), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral( MutableBorrowingLiteral literal, const ShapeIndex& view_root) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(literal.piece(view_root).subshape()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); CopyPieceSubtree(*shape_, &literal.piece(view_root), root_piece_); } MutableBorrowingLiteral::MutableBorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); CHECK(LayoutUtil::HasLayout(*shape_)); CHECK(!shape_->IsTuple()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptr)); } MutableBorrowingLiteral::MutableBorrowingLiteral(absl::Span<char*> src_buf_ptrs, const Shape& shape) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(shape); if (!shape_->IsTuple()) { CHECK_EQ(src_buf_ptrs.size(), 1); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); root_piece_->set_buffer(const_cast<char*>(src_buf_ptrs[0])); } else { CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); for (int i = 0; i < src_buf_ptrs.size(); ++i) { Piece child_piece; const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); child_piece.set_subshape(&src_shape); child_piece.set_buffer(src_buf_ptrs[i]); root_piece_->emplace_back(std::move(child_piece)); } } } MutableBorrowingLiteral::MutableBorrowingLiteral(ShapeTree<char*> src_buf_ptrs) : MutableLiteralBase() { shape_ = std::make_unique<Shape>(src_buf_ptrs.shape()); root_piece_ = new Piece(); root_piece_->set_subshape(shape_.get()); BuildPieceSubtree(*shape_, root_piece_); root_piece_->ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); MutableBorrowingLiteral::~MutableBorrowingLiteral() { if (root_piece_ != nullptr) { delete root_piece_; } } LiteralSlice::LiteralSlice(const LiteralBase& literal) : LiteralBase(), root_piece_(&literal.root_piece()) {} LiteralSlice::LiteralSlice(const LiteralBase& literal, const ShapeIndex& view_root) : LiteralBase(), root_piece_(&literal.piece(view_root)) {} BorrowingLiteral::BorrowingLiteral(const char* src_buf_ptr, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsArray()); CHECK(LayoutUtil::HasLayout(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); root_piece_.set_buffer(const_cast<char*>(src_buf_ptr)); } BorrowingLiteral::BorrowingLiteral(absl::Span<const char* const> src_buf_ptrs, const Shape& shape) : LiteralBase(), shape_(std::make_unique<Shape>(shape)) { CHECK(shape_->IsTuple()); CHECK(!ShapeUtil::IsNestedTuple(*shape_)); CHECK_EQ(src_buf_ptrs.size(), ShapeUtil::TupleElementCount(*shape_)); root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); for (int i = 0, end = src_buf_ptrs.size(); i < end; ++i) { const auto& src_shape = shape_->tuple_shapes(i); CHECK(src_shape.IsArray()); root_piece_.child(i).set_buffer(const_cast<char*>(src_buf_ptrs[i])); } } BorrowingLiteral::BorrowingLiteral(ShapeTree<const char*> src_buf_ptrs) : LiteralBase(), shape_(std::make_unique<Shape>(src_buf_ptrs.shape())) { root_piece_ = Piece(); root_piece_.set_subshape(shape_.get()); BuildPieceSubtree(*shape_, &root_piece_); root_piece_.ForEachMutableSubpiece( [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); }); } [&](const ShapeIndex& index, Piece* piece) { if (ShapeUtil::GetSubshape(*shape_, index).IsTuple()) { DCHECK_EQ(src_buf_ptrs.element(index), nullptr) << "Tuples should not have buffer pointers"; return; } piece->set_buffer(const_cast<char*>(src_buf_ptrs.element(index))); });
#include "xla/literal.h" #include <algorithm> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <limits> #include <random> #include <string> #include <tuple> #include <utility> #include <vector> #include "absl/base/casts.h" #include "absl/hash/hash.h" #include "absl/random/random.h" #include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/types/span.h" #include "xla/array.h" #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/index_util.h" #include "xla/layout.h" #include "xla/layout_util.h" #include "xla/literal_util.h" #include "xla/primitive_util.h" #include "xla/shape.h" #include "xla/shape_tree.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/types.h" #include "xla/util.h" #include "xla/xla_data.pb.h" #include "tsl/lib/core/status_test_util.h" #include "tsl/platform/errors.h" #include "tsl/platform/logging.h" // IWYU pragma: keep #include "tsl/platform/macros.h" #include "tsl/platform/ml_dtypes.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test_benchmark.h" class LiteralUtilTest : public ::testing::Test { protected: LiteralUtilTest() { Array4D<float> arr4d({ // clang-format off { // i0=0 { // i1=0 {1, 2, 3}, // i2=0 {4, 5, 6}, // i2=1 {7, 8, 9}, // i2=2 }, { // i1=1 {11, 12, 13}, {14, 15, 16}, {17, 18, 19}, }, }, { // i0=1 { // i1=0 {101, 102, 103}, {104, 105, 106}, {107, 108, 109}, }, { // i1=1 {201, 202, 203}, // i2=0 {204, 205, 206}, // i2=1 {207, 208, 209}, // i2=2 }, }, // clang-format on }); layout_r2_dim0major_ = LayoutUtil::MakeLayout({1, 0}); layout_r2_dim0minor_ = LayoutUtil::MakeLayout({0, 1}); layout_r3_dim0major_ = LayoutUtil::MakeLayout({2, 1, 0}); layout_r3_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2}); layout_r4_dim0major_ = LayoutUtil::MakeLayout({3, 2, 1, 0}); layout_r4_dim0minor_ = LayoutUtil::MakeLayout({0, 1, 2, 3}); literal_r4_2x2x3x3_dim0major_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0major_); literal_r4_2x2x3x3_dim0minor_ = LiteralUtil::CreateR4FromArray4DWithLayout<float>(arr4d, layout_r4_dim0minor_); } Layout layout_r2_dim0major_; Layout layout_r2_dim0minor_; Layout layout_r3_dim0major_; Layout layout_r3_dim0minor_; Layout layout_r4_dim0major_; Layout layout_r4_dim0minor_; Literal literal_r4_2x2x3x3_dim0major_; Literal literal_r4_2x2x3x3_dim0minor_; }; TEST_F(LiteralUtilTest, ValidProtoWithClearedValues) { auto literal = LiteralUtil::CreateR1<bool>({true, false, true}); LiteralProto proto = literal.ToProto(); EXPECT_EQ(proto.preds_size(), 3); // Clear values. proto.clear_preds(); EXPECT_EQ(proto.preds_size(), 0); absl::Status status = Literal::CreateFromProto(proto, /*prohibit_empty_literal=*/false) .status(); EXPECT_TRUE(status.ok()); }
LoadedExecutableTest_Delete
xla/python/ifrt_proxy/client/executable_test.cc
absl::StatusOr<absl::Cord> ExecuteLoadedHostCallback( xla::ifrt::LoadedHostCallback* loaded_host_callback, absl::Cord operand_buffer) { #if defined(PLATFORM_GOOGLE) auto* pjrt_host_callback = llvm::dyn_cast<PjRtHostSendAndRecvLoadedHostCallback>( loaded_host_callback); if (pjrt_host_callback == nullptr) { return absl::UnimplementedError( "Non-PjRt host callbacks cannot be executed"); } const xla::HostCallback& xla_host_callback = pjrt_host_callback->host_callback(); // The following allocates both operands and results using `aligned_alloc` in // order to (loosely) emulate the XLA implementation where host callbacks are // often called with aligned operand/result buffers. While this may not be // strictly necessary for some callbacks, this reduces the chances of proxied // callbacks behaving differently on a best-effort basis. constexpr int kAlignment = 32; struct Deleter { void operator()(void* p) { free(p); } }; std::vector<std::unique_ptr<char, Deleter>> operands; operands.reserve(xla_host_callback.operands.size()); std::vector<void*> operand_ptrs; operand_ptrs.reserve(xla_host_callback.operands.size()); absl::CordReader reader(operand_buffer); for (const auto& spec : xla_host_callback.operands) { const int64_t size = xla::ShapeUtil::ByteSizeOf(spec.shape); void* p; CHECK_EQ(posix_memalign(&p, kAlignment, size), 0); std::unique_ptr<char, Deleter> buffer(reinterpret_cast<char*>(p)); if (reader.Available() < size) { return absl::InternalError(absl::StrCat( "Buffer overflow while reading host callback execution operands; ", "range: [", reader.Position(), ", ", reader.Position() + size, "), ", "buffer size: ", operand_buffer.size())); } reader.ReadN(size, buffer.get()); operand_ptrs.push_back(buffer.get()); operands.push_back(std::move(buffer)); } if (reader.Available() > 0) { return absl::InternalError(absl::StrCat( "Host callback execution did not consume the entire operand buffer; " "size: ", operand_buffer.size(), "; consumed: ", reader.Available())); } absl::Cord result_buffer; std::vector<void*> result_ptrs; result_ptrs.reserve(xla_host_callback.results.size()); for (const auto& spec : xla_host_callback.results) { const int64_t size = xla::ShapeUtil::ByteSizeOf(spec.shape); void* data; CHECK_EQ(posix_memalign(&data, kAlignment, size), 0); result_ptrs.push_back(data); result_buffer.AppendExternalMemory( absl::string_view(reinterpret_cast<char*>(data), size), data, &free); } TF_RETURN_IF_ERROR( xla_host_callback.callback(result_ptrs.data(), operand_ptrs.data())); return result_buffer; #else return absl::UnimplementedError("ExecuteLoadedHostCallback is unsupported."); #endif } absl::StatusOr<uint64_t> PrepareAndExecuteLoadedHostCallback( ClientHostBufferStore* host_buffer_store, xla::ifrt::LoadedHostCallback* loaded_host_callback, uint64_t operand_handle) { TF_ASSIGN_OR_RETURN(absl::Cord operands, host_buffer_store->Lookup(operand_handle).Await()); absl::Cleanup cleanup = [&]() { host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); }; TF_ASSIGN_OR_RETURN( absl::Cord results, ExecuteLoadedHostCallback(loaded_host_callback, std::move(operands))); const uint64_t result_handle = host_buffer_store->NextHandle(); TF_RETURN_IF_ERROR(host_buffer_store->Store(result_handle, results).Await()); return result_handle; } absl::Cleanup cleanup = [&]() { host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); }; host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); LoadedExecutable::LoadedExecutable( xla::ifrt::Client* client, std::shared_ptr<RpcHelper> rpc_helper, uint64_t handle, std::string name, int num_devices, std::vector<xla::ifrt::LoadedExecutable::LogicalDeviceIds> addressable_device_logical_device_ids, std::vector<xla::ifrt::Device*> addressable_devices, absl::StatusOr<std::optional<std::string>> fingerprint, Future<> ready_future, std::vector<tsl::RCReference<xla::ifrt::LoadedHostCallback>> loaded_host_callbacks, std::vector<uint64_t> loaded_host_callback_handles) : client_(client), rpc_helper_(std::move(rpc_helper)), handle_(handle), name_(std::move(name)), num_devices_(num_devices), addressable_device_logical_device_ids_( std::move(addressable_device_logical_device_ids)), addressable_devices_(std::move(addressable_devices)), fingerprint_(std::move(fingerprint)), ready_future_(std::move(ready_future)) { // Start host callback pollers. CHECK_EQ(loaded_host_callbacks.size(), loaded_host_callback_handles.size()); if (!loaded_host_callbacks.empty()) { for (int i = 0; i < loaded_host_callbacks.size(); ++i) { PollLoadedHostCallback(loaded_host_callback_handles[i], loaded_host_callbacks[i]); } } // Asynchronously fetch shardings. Since users of `LoadedExecutable` typically // require sharding information to invoke the executable, it is beneficial to // eagerly schedule this fetch since, in some implementations, it may take a // long time for sharding information to be available. auto promise = Future<std::shared_ptr<Metadata>>::CreatePromise(); metadata_future_ = Future<std::shared_ptr<Metadata>>(promise); auto req = std::make_unique<LoadedExecutableMetadataRequest>(); req->set_loaded_executable_handle(handle_); auto on_done = [promise]( absl::StatusOr<std::shared_ptr<LoadedExecutableMetadataResponse>> response) mutable { if (!response.ok()) { LOG(ERROR) << "LoadedExecutableMetadata: Got " << response.status(); promise.Set(response.status()); return; } auto info = std::make_shared<Metadata>(); if (response.value()->has_parameter_shardings()) { const auto& p = response.value()->parameter_shardings().shardings(); info->parameter_shardings.emplace(p.begin(), p.end()); } if (response.value()->has_output_shardings()) { const auto& o = response.value()->output_shardings().shardings(); info->output_shardings.emplace(o.begin(), o.end()); } auto parse_layouts = [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; if (response.value()->has_parameter_layouts_list()) { info->parameter_layouts = parse_layouts(response.value()->parameter_layouts_list()); } else if (response.value()->has_parameter_layouts_error()) { info->parameter_layouts = tsl::StatusFromProto(response.value()->parameter_layouts_error()); } else { info->parameter_layouts = absl::UnimplementedError( "IFRT Proxy server did not return parameter layouts"); } if (response.value()->has_output_layouts_list()) { info->output_layouts = parse_layouts(response.value()->output_layouts_list()); } else if (response.value()->has_output_layouts_error()) { info->output_layouts = tsl::StatusFromProto(response.value()->output_layouts_error()); } else { info->output_layouts = absl::UnimplementedError( "IFRT Proxy server did not return output layouts"); } if (const absl::Status s = tsl::StatusFromProto( response.value()->output_memory_kinds().status()); !s.ok()) { info->output_memory_kinds = s; } else { std::vector<std::vector<absl::string_view>> output_memory_kinds; for (const auto& list : response.value()->output_memory_kinds().memory_kind_lists()) { std::vector<absl::string_view> kinds; kinds.reserve(list.memory_kinds_size()); for (const absl::string_view kind : list.memory_kinds()) { const auto it = info->memory_kinds.insert(std::string(kind)).first; kinds.push_back(*it); } output_memory_kinds.push_back(std::move(kinds)); } info->output_memory_kinds = std::move(output_memory_kinds); } promise.Set(std::move(info)); }; rpc_helper_->LoadedExecutableMetadata(std::move(req)) .OnReady(std::move(on_done)); } [promise]( absl::StatusOr<std::shared_ptr<LoadedExecutableMetadataResponse>> response) mutable { if (!response.ok()) { LOG(ERROR) << "LoadedExecutableMetadata: Got " << response.status(); promise.Set(response.status()); return; } auto info = std::make_shared<Metadata>(); if (response.value()->has_parameter_shardings()) { const auto& p = response.value()->parameter_shardings().shardings(); info->parameter_shardings.emplace(p.begin(), p.end()); } if (response.value()->has_output_shardings()) { const auto& o = response.value()->output_shardings().shardings(); info->output_shardings.emplace(o.begin(), o.end()); } auto parse_layouts = [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; if (response.value()->has_parameter_layouts_list()) { info->parameter_layouts = parse_layouts(response.value()->parameter_layouts_list()); } else if (response.value()->has_parameter_layouts_error()) { info->parameter_layouts = tsl::StatusFromProto(response.value()->parameter_layouts_error()); } else { info->parameter_layouts = absl::UnimplementedError( "IFRT Proxy server did not return parameter layouts"); } if (response.value()->has_output_layouts_list()) { info->output_layouts = parse_layouts(response.value()->output_layouts_list()); } else if (response.value()->has_output_layouts_error()) { info->output_layouts = tsl::StatusFromProto(response.value()->output_layouts_error()); } else { info->output_layouts = absl::UnimplementedError( "IFRT Proxy server did not return output layouts"); } if (const absl::Status s = tsl::StatusFromProto( response.value()->output_memory_kinds().status()); !s.ok()) { info->output_memory_kinds = s; } else { std::vector<std::vector<absl::string_view>> output_memory_kinds; for (const auto& list : response.value()->output_memory_kinds().memory_kind_lists()) { std::vector<absl::string_view> kinds; kinds.reserve(list.memory_kinds_size()); for (const absl::string_view kind : list.memory_kinds()) { const auto it = info->memory_kinds.insert(std::string(kind)).first; kinds.push_back(*it); } output_memory_kinds.push_back(std::move(kinds)); } info->output_memory_kinds = std::move(output_memory_kinds); } promise.Set(std::move(info)); }; [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; LoadedExecutable::~LoadedExecutable() { auto req = std::make_unique<LoadedExecutableDestructRequest>(); req->set_loaded_executable_handle(handle_); rpc_helper_->LoadedExecutableDestruct(std::move(req)) .OnReady( [](absl::StatusOr<std::shared_ptr<LoadedExecutableDestructResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to destroy `LoadedExecutable`: " << response.status(); } }); } [](absl::StatusOr<std::shared_ptr<LoadedExecutableDestructResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to destroy `LoadedExecutable`: " << response.status(); } }); xla::ifrt::Client* LoadedExecutable::client() const { return client_; } absl::string_view LoadedExecutable::name() const { return name_; } absl::StatusOr<std::optional<std::string>> LoadedExecutable::Fingerprint() const { return fingerprint_; } absl::StatusOr<std::string> LoadedExecutable::Serialize() const { return absl::UnimplementedError( "IFRT service executable does not support `Serialize` since the " "underlying serialization format is not stable"); } Future<> LoadedExecutable::GetReadyFuture() const { return ready_future_; } int LoadedExecutable::num_devices() const { return num_devices_; } int64_t LoadedExecutable::SizeOfGeneratedCodeInBytes() const { LOG(FATAL) << "Unimplemented"; } absl::StatusOr<CompiledMemoryStats> LoadedExecutable::GetCompiledMemoryStats() const { return absl::UnimplementedError("Unimplemented"); } std::optional<std::vector<OpSharding>> LoadedExecutable::GetParameterShardings() const { auto info = metadata_future_.Await(); if (!info.ok()) { return std::nullopt; } return (*info)->parameter_shardings; } std::optional<std::vector<OpSharding>> LoadedExecutable::GetOutputShardings() const { auto info = metadata_future_.Await(); if (!info.ok()) { return std::nullopt; } return (*info)->output_shardings; } LoadedExecutable::GetParameterLayouts() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); TF_RETURN_IF_ERROR(info->parameter_layouts.status()); std::vector<std::unique_ptr<Layout>> result; result.reserve(info->parameter_layouts->size()); for (const xla::Layout& layout : *info->parameter_layouts) { result.push_back(std::make_unique<xla::PjRtXlaLayout>(layout)); } return result; } LoadedExecutable::GetOutputLayouts() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); TF_RETURN_IF_ERROR(info->output_layouts.status()); std::vector<std::unique_ptr<Layout>> result; result.reserve(info->output_layouts->size()); for (const xla::Layout& layout : *info->output_layouts) { result.push_back(std::make_unique<xla::PjRtXlaLayout>(layout)); } return result; } LoadedExecutable::GetOutputMemoryKinds() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); return info->output_memory_kinds; } LoadedExecutable::GetHloModules() const { return absl::UnimplementedError( "IFRT service does not support LoadedExecutable::GetHloModules() since " "HloModule does not provide stable serialization"); } LoadedExecutable::GetCostAnalysis() const { return absl::UnimplementedError("Unimplemented"); } LoadedExecutable::Execute(absl::Span<tsl::RCReference<xla::ifrt::Array>> args, const ExecuteOptions& options, std::optional<xla::ifrt::DeviceList> devices) { auto req = std::make_unique<LoadedExecutableExecuteRequest>(); req->set_loaded_executable_handle(handle_); for (const auto& arg : args) { auto* array = llvm::dyn_cast_or_null<Array>(arg.get()); if (array == nullptr) { return absl::InvalidArgumentError( "Invalid IFRT array type provided to `LoadedExecutable::Execute`"); } req->add_args_handles(array->handle().handle); } TF_ASSIGN_OR_RETURN(*req->mutable_execute_options(), options.ToProto()); if (devices.has_value()) { for (const auto* device : *devices) { req->add_device_ids(device->Id().value()); } } TF_ASSIGN_OR_RETURN( std::shared_ptr<LoadedExecutableExecuteResponse> response, rpc_helper_->LoadedExecutableExecute(std::move(req)).Await()); // NOTE: All future and array handles in `response` must have an owner // locally, or be requested to be destructed remotely, before returning. xla::ifrt::LoadedExecutable::ExecuteResult result; // Populate the execution status future. `CheckFuture` deletes the server-side // futures after its completion. result.status = rpc_helper_->CheckFuture(response->status_handle()); // Create output arrays. The cleanup logic ensures that all handles are // properly cleaned up on early return. absl::Cleanup cleanup = [&]() { int index = result.outputs.size(); result.outputs.clear(); // Cleaned up by `~Array()`. for (; index < response->outputs_size(); ++index) { Array::Destruct(rpc_helper_.get(), ArrayHandle{response->outputs(index).array_handle()}); } }; const auto lookup_device = absl::bind_front(&Client::LookupDevice, client()); for (const auto& output : response->outputs()) { TF_ASSIGN_OR_RETURN(DType dtype, DType::FromProto(output.dtype())); TF_ASSIGN_OR_RETURN(Shape shape, Shape::FromProto(output.shape())); TF_ASSIGN_OR_RETURN(auto sharding, Sharding::FromProto(lookup_device, output.sharding())); result.outputs.push_back(tsl::MakeRef<Array>( client(), rpc_helper_, dtype, std::move(shape), std::move(sharding), ArrayHandle{output.array_handle()})); } std::move(cleanup).Cancel(); return result; } absl::Cleanup cleanup = [&]() { int index = result.outputs.size(); result.outputs.clear(); // Cleaned up by `~Array()`. for (; index < response->outputs_size(); ++index) { Array::Destruct(rpc_helper_.get(), ArrayHandle{response->outputs(index).array_handle()}); } }; Future<> LoadedExecutable::Delete() { auto req = std::make_unique<LoadedExecutableDeleteRequest>(); req->set_loaded_executable_handle(handle_); absl::StatusOr<std::shared_ptr<LoadedExecutableDeleteResponse>> response = rpc_helper_->LoadedExecutableDelete(std::move(req)).Await(); if (!response.ok()) { return Future<>(response.status()); } return rpc_helper_->CheckFuture((*response)->future_handle()); } bool LoadedExecutable::IsDeleted() const { auto req = std::make_unique<LoadedExecutableIsDeletedRequest>(); req->set_loaded_executable_handle(handle_); absl::StatusOr<std::shared_ptr<LoadedExecutableIsDeletedResponse>> response = rpc_helper_->LoadedExecutableIsDeleted(std::move(req)).Await(); if (!response.ok()) { LOG(ERROR) << "Failed to query the deletion status of `LoadedExecutable`: " << response.status(); return false; } return (*response)->is_deleted(); } LoadedExecutable::addressable_device_logical_ids() const { return addressable_device_logical_device_ids_; } absl::Span<xla::ifrt::Device* const> LoadedExecutable::addressable_devices() const { return addressable_devices_; } void LoadedExecutable::PollLoadedHostCallback( uint64_t handle, tsl::RCReference<xla::ifrt::LoadedHostCallback> loaded_host_callback) { // Note: individual host callbacks may live longer than the executable as the // destruction of an IFRT executable is not required to block until all // in-flight executions are complete. Therefore, the following lambda must not // capture `this` and is scheduled on the default thread pool. auto f = [rpc_helper = rpc_helper_, handle, loaded_host_callback = std::move(loaded_host_callback)]() { while (true) { const uint64_t operand_handle = rpc_helper->host_buffer_store()->NextHandle(); auto poll_req = std::make_unique<LoadedHostCallbackPollRequest>(); poll_req->set_loaded_host_callback_handle(handle); poll_req->set_operand_host_buffer_handle(operand_handle); auto response = rpc_helper->LoadedHostCallbackPoll(std::move(poll_req)).Await(); if (!response.ok()) { LOG_EVERY_N_SEC(ERROR, 60) << "Failed to poll host callback execution: " << response.status(); continue; } if (!(*response)->has_host_callback_execution_handle()) { // The host callback is destructed from the server. break; } auto ret_req = std::make_unique<LoadedHostCallbackReturnRequest>(); ret_req->set_host_callback_execution_handle( (*response)->host_callback_execution_handle()); absl::StatusOr<uint64_t> result_handle = PrepareAndExecuteLoadedHostCallback( rpc_helper->host_buffer_store().get(), loaded_host_callback.get(), operand_handle); if (result_handle.ok()) { ret_req->set_result_host_buffer_handle(*result_handle); } else { *ret_req->mutable_error() = tsl::StatusToProto(result_handle.status()); } rpc_helper->LoadedHostCallbackReturn(std::move(ret_req)) .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } }); } }; static auto* global_pool = new tsl::thread::ThreadPool( tsl::Env::Default(), GetThreadOptions(), "XLAIFRTProxy", std::min(16, tsl::port::MaxParallelism())); global_pool->Schedule(std::move(f)); } auto f = [rpc_helper = rpc_helper_, handle, loaded_host_callback = std::move(loaded_host_callback)]() { while (true) { const uint64_t operand_handle = rpc_helper->host_buffer_store()->NextHandle(); auto poll_req = std::make_unique<LoadedHostCallbackPollRequest>(); poll_req->set_loaded_host_callback_handle(handle); poll_req->set_operand_host_buffer_handle(operand_handle); auto response = rpc_helper->LoadedHostCallbackPoll(std::move(poll_req)).Await(); if (!response.ok()) { LOG_EVERY_N_SEC(ERROR, 60) << "Failed to poll host callback execution: " << response.status(); continue; } if (!(*response)->has_host_callback_execution_handle()) { // The host callback is destructed from the server. break; } auto ret_req = std::make_unique<LoadedHostCallbackReturnRequest>(); ret_req->set_host_callback_execution_handle( (*response)->host_callback_execution_handle()); absl::StatusOr<uint64_t> result_handle = PrepareAndExecuteLoadedHostCallback( rpc_helper->host_buffer_store().get(), loaded_host_callback.get(), operand_handle); if (result_handle.ok()) { ret_req->set_result_host_buffer_handle(*result_handle); } else { *ret_req->mutable_error() = tsl::StatusToProto(result_handle.status()); } rpc_helper->LoadedHostCallbackReturn(std::move(ret_req)) .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } }); } }; .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } });
#include "xla/python/ifrt_proxy/client/executable.h" #include <cstdint> #include <memory> #include <vector> #include <gmock/gmock.h> #include <gtest/gtest.h> #include "absl/status/status.h" #include "absl/types/span.h" #include "llvm/Support/Casting.h" #include "xla/layout_util.h" #include "xla/pjrt/pjrt_common.h" #include "xla/pjrt/pjrt_layout.h" #include "xla/python/ifrt/array.h" #include "xla/python/ifrt/device.h" #include "xla/python/ifrt/dtype.h" #include "xla/python/ifrt/executable.h" #include "xla/python/ifrt/future.h" #include "xla/python/ifrt/memory.h" #include "xla/python/ifrt/mock.h" #include "xla/python/ifrt/shape.h" #include "xla/python/ifrt/sharding.h" #include "xla/python/ifrt_proxy/client/array.h" #include "xla/python/ifrt_proxy/client/client_session.h" #include "xla/python/ifrt_proxy/client/host_buffer.h" #include "xla/python/ifrt_proxy/client/mock_client_session.h" #include "xla/python/ifrt_proxy/client/mock_host_buffer.h" #include "xla/python/ifrt_proxy/client/rpc_helper.h" #include "xla/python/ifrt_proxy/client/version.h" #include "xla/python/ifrt_proxy/common/ifrt_service.pb.h" #include "xla/python/ifrt_proxy/common/types.h" #include "xla/tsl/concurrency/ref_count.h" #include "tsl/platform/casts.h" #include "tsl/platform/protobuf.h" // IWYU pragma: keep #include "tsl/platform/status_matchers.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test.h" class LoadedExecutableTest : public ::testing::Test { protected: void SetUp() override { session_ = std::make_shared<MockClientSession>(); rpc_helper_ = std::make_shared<RpcHelper>(Version(), session_); host_buffer_store_ = std::make_shared<MockClientHostBufferStore>(); rpc_helper_->set_host_buffer_store(host_buffer_store_); // Default handler that ignores all uninteresting requests, but still // invokes the callback in order to avoid hanging the caller forever. EXPECT_CALL(*session_, Enqueue(_)) .WillRepeatedly(Return(Future<ClientSession::Response>( absl::InternalError("Request has no mock handlers")))); } std::shared_ptr<MockClientSession> session_; std::shared_ptr<RpcHelper> rpc_helper_; std::shared_ptr<ClientHostBufferStore> host_buffer_store_; };
LoadedExecutableTest_Execute
xla/python/ifrt_proxy/client/executable_test.cc
absl::StatusOr<absl::Cord> ExecuteLoadedHostCallback( xla::ifrt::LoadedHostCallback* loaded_host_callback, absl::Cord operand_buffer) { #if defined(PLATFORM_GOOGLE) auto* pjrt_host_callback = llvm::dyn_cast<PjRtHostSendAndRecvLoadedHostCallback>( loaded_host_callback); if (pjrt_host_callback == nullptr) { return absl::UnimplementedError( "Non-PjRt host callbacks cannot be executed"); } const xla::HostCallback& xla_host_callback = pjrt_host_callback->host_callback(); // The following allocates both operands and results using `aligned_alloc` in // order to (loosely) emulate the XLA implementation where host callbacks are // often called with aligned operand/result buffers. While this may not be // strictly necessary for some callbacks, this reduces the chances of proxied // callbacks behaving differently on a best-effort basis. constexpr int kAlignment = 32; struct Deleter { void operator()(void* p) { free(p); } }; std::vector<std::unique_ptr<char, Deleter>> operands; operands.reserve(xla_host_callback.operands.size()); std::vector<void*> operand_ptrs; operand_ptrs.reserve(xla_host_callback.operands.size()); absl::CordReader reader(operand_buffer); for (const auto& spec : xla_host_callback.operands) { const int64_t size = xla::ShapeUtil::ByteSizeOf(spec.shape); void* p; CHECK_EQ(posix_memalign(&p, kAlignment, size), 0); std::unique_ptr<char, Deleter> buffer(reinterpret_cast<char*>(p)); if (reader.Available() < size) { return absl::InternalError(absl::StrCat( "Buffer overflow while reading host callback execution operands; ", "range: [", reader.Position(), ", ", reader.Position() + size, "), ", "buffer size: ", operand_buffer.size())); } reader.ReadN(size, buffer.get()); operand_ptrs.push_back(buffer.get()); operands.push_back(std::move(buffer)); } if (reader.Available() > 0) { return absl::InternalError(absl::StrCat( "Host callback execution did not consume the entire operand buffer; " "size: ", operand_buffer.size(), "; consumed: ", reader.Available())); } absl::Cord result_buffer; std::vector<void*> result_ptrs; result_ptrs.reserve(xla_host_callback.results.size()); for (const auto& spec : xla_host_callback.results) { const int64_t size = xla::ShapeUtil::ByteSizeOf(spec.shape); void* data; CHECK_EQ(posix_memalign(&data, kAlignment, size), 0); result_ptrs.push_back(data); result_buffer.AppendExternalMemory( absl::string_view(reinterpret_cast<char*>(data), size), data, &free); } TF_RETURN_IF_ERROR( xla_host_callback.callback(result_ptrs.data(), operand_ptrs.data())); return result_buffer; #else return absl::UnimplementedError("ExecuteLoadedHostCallback is unsupported."); #endif } absl::StatusOr<uint64_t> PrepareAndExecuteLoadedHostCallback( ClientHostBufferStore* host_buffer_store, xla::ifrt::LoadedHostCallback* loaded_host_callback, uint64_t operand_handle) { TF_ASSIGN_OR_RETURN(absl::Cord operands, host_buffer_store->Lookup(operand_handle).Await()); absl::Cleanup cleanup = [&]() { host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); }; TF_ASSIGN_OR_RETURN( absl::Cord results, ExecuteLoadedHostCallback(loaded_host_callback, std::move(operands))); const uint64_t result_handle = host_buffer_store->NextHandle(); TF_RETURN_IF_ERROR(host_buffer_store->Store(result_handle, results).Await()); return result_handle; } absl::Cleanup cleanup = [&]() { host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); }; host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); LoadedExecutable::LoadedExecutable( xla::ifrt::Client* client, std::shared_ptr<RpcHelper> rpc_helper, uint64_t handle, std::string name, int num_devices, std::vector<xla::ifrt::LoadedExecutable::LogicalDeviceIds> addressable_device_logical_device_ids, std::vector<xla::ifrt::Device*> addressable_devices, absl::StatusOr<std::optional<std::string>> fingerprint, Future<> ready_future, std::vector<tsl::RCReference<xla::ifrt::LoadedHostCallback>> loaded_host_callbacks, std::vector<uint64_t> loaded_host_callback_handles) : client_(client), rpc_helper_(std::move(rpc_helper)), handle_(handle), name_(std::move(name)), num_devices_(num_devices), addressable_device_logical_device_ids_( std::move(addressable_device_logical_device_ids)), addressable_devices_(std::move(addressable_devices)), fingerprint_(std::move(fingerprint)), ready_future_(std::move(ready_future)) { // Start host callback pollers. CHECK_EQ(loaded_host_callbacks.size(), loaded_host_callback_handles.size()); if (!loaded_host_callbacks.empty()) { for (int i = 0; i < loaded_host_callbacks.size(); ++i) { PollLoadedHostCallback(loaded_host_callback_handles[i], loaded_host_callbacks[i]); } } // Asynchronously fetch shardings. Since users of `LoadedExecutable` typically // require sharding information to invoke the executable, it is beneficial to // eagerly schedule this fetch since, in some implementations, it may take a // long time for sharding information to be available. auto promise = Future<std::shared_ptr<Metadata>>::CreatePromise(); metadata_future_ = Future<std::shared_ptr<Metadata>>(promise); auto req = std::make_unique<LoadedExecutableMetadataRequest>(); req->set_loaded_executable_handle(handle_); auto on_done = [promise]( absl::StatusOr<std::shared_ptr<LoadedExecutableMetadataResponse>> response) mutable { if (!response.ok()) { LOG(ERROR) << "LoadedExecutableMetadata: Got " << response.status(); promise.Set(response.status()); return; } auto info = std::make_shared<Metadata>(); if (response.value()->has_parameter_shardings()) { const auto& p = response.value()->parameter_shardings().shardings(); info->parameter_shardings.emplace(p.begin(), p.end()); } if (response.value()->has_output_shardings()) { const auto& o = response.value()->output_shardings().shardings(); info->output_shardings.emplace(o.begin(), o.end()); } auto parse_layouts = [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; if (response.value()->has_parameter_layouts_list()) { info->parameter_layouts = parse_layouts(response.value()->parameter_layouts_list()); } else if (response.value()->has_parameter_layouts_error()) { info->parameter_layouts = tsl::StatusFromProto(response.value()->parameter_layouts_error()); } else { info->parameter_layouts = absl::UnimplementedError( "IFRT Proxy server did not return parameter layouts"); } if (response.value()->has_output_layouts_list()) { info->output_layouts = parse_layouts(response.value()->output_layouts_list()); } else if (response.value()->has_output_layouts_error()) { info->output_layouts = tsl::StatusFromProto(response.value()->output_layouts_error()); } else { info->output_layouts = absl::UnimplementedError( "IFRT Proxy server did not return output layouts"); } if (const absl::Status s = tsl::StatusFromProto( response.value()->output_memory_kinds().status()); !s.ok()) { info->output_memory_kinds = s; } else { std::vector<std::vector<absl::string_view>> output_memory_kinds; for (const auto& list : response.value()->output_memory_kinds().memory_kind_lists()) { std::vector<absl::string_view> kinds; kinds.reserve(list.memory_kinds_size()); for (const absl::string_view kind : list.memory_kinds()) { const auto it = info->memory_kinds.insert(std::string(kind)).first; kinds.push_back(*it); } output_memory_kinds.push_back(std::move(kinds)); } info->output_memory_kinds = std::move(output_memory_kinds); } promise.Set(std::move(info)); }; rpc_helper_->LoadedExecutableMetadata(std::move(req)) .OnReady(std::move(on_done)); } [promise]( absl::StatusOr<std::shared_ptr<LoadedExecutableMetadataResponse>> response) mutable { if (!response.ok()) { LOG(ERROR) << "LoadedExecutableMetadata: Got " << response.status(); promise.Set(response.status()); return; } auto info = std::make_shared<Metadata>(); if (response.value()->has_parameter_shardings()) { const auto& p = response.value()->parameter_shardings().shardings(); info->parameter_shardings.emplace(p.begin(), p.end()); } if (response.value()->has_output_shardings()) { const auto& o = response.value()->output_shardings().shardings(); info->output_shardings.emplace(o.begin(), o.end()); } auto parse_layouts = [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; if (response.value()->has_parameter_layouts_list()) { info->parameter_layouts = parse_layouts(response.value()->parameter_layouts_list()); } else if (response.value()->has_parameter_layouts_error()) { info->parameter_layouts = tsl::StatusFromProto(response.value()->parameter_layouts_error()); } else { info->parameter_layouts = absl::UnimplementedError( "IFRT Proxy server did not return parameter layouts"); } if (response.value()->has_output_layouts_list()) { info->output_layouts = parse_layouts(response.value()->output_layouts_list()); } else if (response.value()->has_output_layouts_error()) { info->output_layouts = tsl::StatusFromProto(response.value()->output_layouts_error()); } else { info->output_layouts = absl::UnimplementedError( "IFRT Proxy server did not return output layouts"); } if (const absl::Status s = tsl::StatusFromProto( response.value()->output_memory_kinds().status()); !s.ok()) { info->output_memory_kinds = s; } else { std::vector<std::vector<absl::string_view>> output_memory_kinds; for (const auto& list : response.value()->output_memory_kinds().memory_kind_lists()) { std::vector<absl::string_view> kinds; kinds.reserve(list.memory_kinds_size()); for (const absl::string_view kind : list.memory_kinds()) { const auto it = info->memory_kinds.insert(std::string(kind)).first; kinds.push_back(*it); } output_memory_kinds.push_back(std::move(kinds)); } info->output_memory_kinds = std::move(output_memory_kinds); } promise.Set(std::move(info)); }; [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; LoadedExecutable::~LoadedExecutable() { auto req = std::make_unique<LoadedExecutableDestructRequest>(); req->set_loaded_executable_handle(handle_); rpc_helper_->LoadedExecutableDestruct(std::move(req)) .OnReady( [](absl::StatusOr<std::shared_ptr<LoadedExecutableDestructResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to destroy `LoadedExecutable`: " << response.status(); } }); } [](absl::StatusOr<std::shared_ptr<LoadedExecutableDestructResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to destroy `LoadedExecutable`: " << response.status(); } }); xla::ifrt::Client* LoadedExecutable::client() const { return client_; } absl::string_view LoadedExecutable::name() const { return name_; } absl::StatusOr<std::optional<std::string>> LoadedExecutable::Fingerprint() const { return fingerprint_; } absl::StatusOr<std::string> LoadedExecutable::Serialize() const { return absl::UnimplementedError( "IFRT service executable does not support `Serialize` since the " "underlying serialization format is not stable"); } Future<> LoadedExecutable::GetReadyFuture() const { return ready_future_; } int LoadedExecutable::num_devices() const { return num_devices_; } int64_t LoadedExecutable::SizeOfGeneratedCodeInBytes() const { LOG(FATAL) << "Unimplemented"; } absl::StatusOr<CompiledMemoryStats> LoadedExecutable::GetCompiledMemoryStats() const { return absl::UnimplementedError("Unimplemented"); } std::optional<std::vector<OpSharding>> LoadedExecutable::GetParameterShardings() const { auto info = metadata_future_.Await(); if (!info.ok()) { return std::nullopt; } return (*info)->parameter_shardings; } std::optional<std::vector<OpSharding>> LoadedExecutable::GetOutputShardings() const { auto info = metadata_future_.Await(); if (!info.ok()) { return std::nullopt; } return (*info)->output_shardings; } LoadedExecutable::GetParameterLayouts() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); TF_RETURN_IF_ERROR(info->parameter_layouts.status()); std::vector<std::unique_ptr<Layout>> result; result.reserve(info->parameter_layouts->size()); for (const xla::Layout& layout : *info->parameter_layouts) { result.push_back(std::make_unique<xla::PjRtXlaLayout>(layout)); } return result; } LoadedExecutable::GetOutputLayouts() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); TF_RETURN_IF_ERROR(info->output_layouts.status()); std::vector<std::unique_ptr<Layout>> result; result.reserve(info->output_layouts->size()); for (const xla::Layout& layout : *info->output_layouts) { result.push_back(std::make_unique<xla::PjRtXlaLayout>(layout)); } return result; } LoadedExecutable::GetOutputMemoryKinds() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); return info->output_memory_kinds; } LoadedExecutable::GetHloModules() const { return absl::UnimplementedError( "IFRT service does not support LoadedExecutable::GetHloModules() since " "HloModule does not provide stable serialization"); } LoadedExecutable::GetCostAnalysis() const { return absl::UnimplementedError("Unimplemented"); } LoadedExecutable::Execute(absl::Span<tsl::RCReference<xla::ifrt::Array>> args, const ExecuteOptions& options, std::optional<xla::ifrt::DeviceList> devices) { auto req = std::make_unique<LoadedExecutableExecuteRequest>(); req->set_loaded_executable_handle(handle_); for (const auto& arg : args) { auto* array = llvm::dyn_cast_or_null<Array>(arg.get()); if (array == nullptr) { return absl::InvalidArgumentError( "Invalid IFRT array type provided to `LoadedExecutable::Execute`"); } req->add_args_handles(array->handle().handle); } TF_ASSIGN_OR_RETURN(*req->mutable_execute_options(), options.ToProto()); if (devices.has_value()) { for (const auto* device : *devices) { req->add_device_ids(device->Id().value()); } } TF_ASSIGN_OR_RETURN( std::shared_ptr<LoadedExecutableExecuteResponse> response, rpc_helper_->LoadedExecutableExecute(std::move(req)).Await()); // NOTE: All future and array handles in `response` must have an owner // locally, or be requested to be destructed remotely, before returning. xla::ifrt::LoadedExecutable::ExecuteResult result; // Populate the execution status future. `CheckFuture` deletes the server-side // futures after its completion. result.status = rpc_helper_->CheckFuture(response->status_handle()); // Create output arrays. The cleanup logic ensures that all handles are // properly cleaned up on early return. absl::Cleanup cleanup = [&]() { int index = result.outputs.size(); result.outputs.clear(); // Cleaned up by `~Array()`. for (; index < response->outputs_size(); ++index) { Array::Destruct(rpc_helper_.get(), ArrayHandle{response->outputs(index).array_handle()}); } }; const auto lookup_device = absl::bind_front(&Client::LookupDevice, client()); for (const auto& output : response->outputs()) { TF_ASSIGN_OR_RETURN(DType dtype, DType::FromProto(output.dtype())); TF_ASSIGN_OR_RETURN(Shape shape, Shape::FromProto(output.shape())); TF_ASSIGN_OR_RETURN(auto sharding, Sharding::FromProto(lookup_device, output.sharding())); result.outputs.push_back(tsl::MakeRef<Array>( client(), rpc_helper_, dtype, std::move(shape), std::move(sharding), ArrayHandle{output.array_handle()})); } std::move(cleanup).Cancel(); return result; } absl::Cleanup cleanup = [&]() { int index = result.outputs.size(); result.outputs.clear(); // Cleaned up by `~Array()`. for (; index < response->outputs_size(); ++index) { Array::Destruct(rpc_helper_.get(), ArrayHandle{response->outputs(index).array_handle()}); } }; Future<> LoadedExecutable::Delete() { auto req = std::make_unique<LoadedExecutableDeleteRequest>(); req->set_loaded_executable_handle(handle_); absl::StatusOr<std::shared_ptr<LoadedExecutableDeleteResponse>> response = rpc_helper_->LoadedExecutableDelete(std::move(req)).Await(); if (!response.ok()) { return Future<>(response.status()); } return rpc_helper_->CheckFuture((*response)->future_handle()); } bool LoadedExecutable::IsDeleted() const { auto req = std::make_unique<LoadedExecutableIsDeletedRequest>(); req->set_loaded_executable_handle(handle_); absl::StatusOr<std::shared_ptr<LoadedExecutableIsDeletedResponse>> response = rpc_helper_->LoadedExecutableIsDeleted(std::move(req)).Await(); if (!response.ok()) { LOG(ERROR) << "Failed to query the deletion status of `LoadedExecutable`: " << response.status(); return false; } return (*response)->is_deleted(); } LoadedExecutable::addressable_device_logical_ids() const { return addressable_device_logical_device_ids_; } absl::Span<xla::ifrt::Device* const> LoadedExecutable::addressable_devices() const { return addressable_devices_; } void LoadedExecutable::PollLoadedHostCallback( uint64_t handle, tsl::RCReference<xla::ifrt::LoadedHostCallback> loaded_host_callback) { // Note: individual host callbacks may live longer than the executable as the // destruction of an IFRT executable is not required to block until all // in-flight executions are complete. Therefore, the following lambda must not // capture `this` and is scheduled on the default thread pool. auto f = [rpc_helper = rpc_helper_, handle, loaded_host_callback = std::move(loaded_host_callback)]() { while (true) { const uint64_t operand_handle = rpc_helper->host_buffer_store()->NextHandle(); auto poll_req = std::make_unique<LoadedHostCallbackPollRequest>(); poll_req->set_loaded_host_callback_handle(handle); poll_req->set_operand_host_buffer_handle(operand_handle); auto response = rpc_helper->LoadedHostCallbackPoll(std::move(poll_req)).Await(); if (!response.ok()) { LOG_EVERY_N_SEC(ERROR, 60) << "Failed to poll host callback execution: " << response.status(); continue; } if (!(*response)->has_host_callback_execution_handle()) { // The host callback is destructed from the server. break; } auto ret_req = std::make_unique<LoadedHostCallbackReturnRequest>(); ret_req->set_host_callback_execution_handle( (*response)->host_callback_execution_handle()); absl::StatusOr<uint64_t> result_handle = PrepareAndExecuteLoadedHostCallback( rpc_helper->host_buffer_store().get(), loaded_host_callback.get(), operand_handle); if (result_handle.ok()) { ret_req->set_result_host_buffer_handle(*result_handle); } else { *ret_req->mutable_error() = tsl::StatusToProto(result_handle.status()); } rpc_helper->LoadedHostCallbackReturn(std::move(ret_req)) .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } }); } }; static auto* global_pool = new tsl::thread::ThreadPool( tsl::Env::Default(), GetThreadOptions(), "XLAIFRTProxy", std::min(16, tsl::port::MaxParallelism())); global_pool->Schedule(std::move(f)); } auto f = [rpc_helper = rpc_helper_, handle, loaded_host_callback = std::move(loaded_host_callback)]() { while (true) { const uint64_t operand_handle = rpc_helper->host_buffer_store()->NextHandle(); auto poll_req = std::make_unique<LoadedHostCallbackPollRequest>(); poll_req->set_loaded_host_callback_handle(handle); poll_req->set_operand_host_buffer_handle(operand_handle); auto response = rpc_helper->LoadedHostCallbackPoll(std::move(poll_req)).Await(); if (!response.ok()) { LOG_EVERY_N_SEC(ERROR, 60) << "Failed to poll host callback execution: " << response.status(); continue; } if (!(*response)->has_host_callback_execution_handle()) { // The host callback is destructed from the server. break; } auto ret_req = std::make_unique<LoadedHostCallbackReturnRequest>(); ret_req->set_host_callback_execution_handle( (*response)->host_callback_execution_handle()); absl::StatusOr<uint64_t> result_handle = PrepareAndExecuteLoadedHostCallback( rpc_helper->host_buffer_store().get(), loaded_host_callback.get(), operand_handle); if (result_handle.ok()) { ret_req->set_result_host_buffer_handle(*result_handle); } else { *ret_req->mutable_error() = tsl::StatusToProto(result_handle.status()); } rpc_helper->LoadedHostCallbackReturn(std::move(ret_req)) .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } }); } }; .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } });
#include "xla/python/ifrt_proxy/client/executable.h" #include <cstdint> #include <memory> #include <vector> #include <gmock/gmock.h> #include <gtest/gtest.h> #include "absl/status/status.h" #include "absl/types/span.h" #include "llvm/Support/Casting.h" #include "xla/layout_util.h" #include "xla/pjrt/pjrt_common.h" #include "xla/pjrt/pjrt_layout.h" #include "xla/python/ifrt/array.h" #include "xla/python/ifrt/device.h" #include "xla/python/ifrt/dtype.h" #include "xla/python/ifrt/executable.h" #include "xla/python/ifrt/future.h" #include "xla/python/ifrt/memory.h" #include "xla/python/ifrt/mock.h" #include "xla/python/ifrt/shape.h" #include "xla/python/ifrt/sharding.h" #include "xla/python/ifrt_proxy/client/array.h" #include "xla/python/ifrt_proxy/client/client_session.h" #include "xla/python/ifrt_proxy/client/host_buffer.h" #include "xla/python/ifrt_proxy/client/mock_client_session.h" #include "xla/python/ifrt_proxy/client/mock_host_buffer.h" #include "xla/python/ifrt_proxy/client/rpc_helper.h" #include "xla/python/ifrt_proxy/client/version.h" #include "xla/python/ifrt_proxy/common/ifrt_service.pb.h" #include "xla/python/ifrt_proxy/common/types.h" #include "xla/tsl/concurrency/ref_count.h" #include "tsl/platform/casts.h" #include "tsl/platform/protobuf.h" // IWYU pragma: keep #include "tsl/platform/status_matchers.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test.h" class LoadedExecutableTest : public ::testing::Test { protected: void SetUp() override { session_ = std::make_shared<MockClientSession>(); rpc_helper_ = std::make_shared<RpcHelper>(Version(), session_); host_buffer_store_ = std::make_shared<MockClientHostBufferStore>(); rpc_helper_->set_host_buffer_store(host_buffer_store_); // Default handler that ignores all uninteresting requests, but still // invokes the callback in order to avoid hanging the caller forever. EXPECT_CALL(*session_, Enqueue(_)) .WillRepeatedly(Return(Future<ClientSession::Response>( absl::InternalError("Request has no mock handlers")))); } std::shared_ptr<MockClientSession> session_; std::shared_ptr<RpcHelper> rpc_helper_; std::shared_ptr<ClientHostBufferStore> host_buffer_store_; };
LoadedExecutableTest_Metadata
xla/python/ifrt_proxy/client/executable_test.cc
absl::StatusOr<absl::Cord> ExecuteLoadedHostCallback( xla::ifrt::LoadedHostCallback* loaded_host_callback, absl::Cord operand_buffer) { #if defined(PLATFORM_GOOGLE) auto* pjrt_host_callback = llvm::dyn_cast<PjRtHostSendAndRecvLoadedHostCallback>( loaded_host_callback); if (pjrt_host_callback == nullptr) { return absl::UnimplementedError( "Non-PjRt host callbacks cannot be executed"); } const xla::HostCallback& xla_host_callback = pjrt_host_callback->host_callback(); // The following allocates both operands and results using `aligned_alloc` in // order to (loosely) emulate the XLA implementation where host callbacks are // often called with aligned operand/result buffers. While this may not be // strictly necessary for some callbacks, this reduces the chances of proxied // callbacks behaving differently on a best-effort basis. constexpr int kAlignment = 32; struct Deleter { void operator()(void* p) { free(p); } }; std::vector<std::unique_ptr<char, Deleter>> operands; operands.reserve(xla_host_callback.operands.size()); std::vector<void*> operand_ptrs; operand_ptrs.reserve(xla_host_callback.operands.size()); absl::CordReader reader(operand_buffer); for (const auto& spec : xla_host_callback.operands) { const int64_t size = xla::ShapeUtil::ByteSizeOf(spec.shape); void* p; CHECK_EQ(posix_memalign(&p, kAlignment, size), 0); std::unique_ptr<char, Deleter> buffer(reinterpret_cast<char*>(p)); if (reader.Available() < size) { return absl::InternalError(absl::StrCat( "Buffer overflow while reading host callback execution operands; ", "range: [", reader.Position(), ", ", reader.Position() + size, "), ", "buffer size: ", operand_buffer.size())); } reader.ReadN(size, buffer.get()); operand_ptrs.push_back(buffer.get()); operands.push_back(std::move(buffer)); } if (reader.Available() > 0) { return absl::InternalError(absl::StrCat( "Host callback execution did not consume the entire operand buffer; " "size: ", operand_buffer.size(), "; consumed: ", reader.Available())); } absl::Cord result_buffer; std::vector<void*> result_ptrs; result_ptrs.reserve(xla_host_callback.results.size()); for (const auto& spec : xla_host_callback.results) { const int64_t size = xla::ShapeUtil::ByteSizeOf(spec.shape); void* data; CHECK_EQ(posix_memalign(&data, kAlignment, size), 0); result_ptrs.push_back(data); result_buffer.AppendExternalMemory( absl::string_view(reinterpret_cast<char*>(data), size), data, &free); } TF_RETURN_IF_ERROR( xla_host_callback.callback(result_ptrs.data(), operand_ptrs.data())); return result_buffer; #else return absl::UnimplementedError("ExecuteLoadedHostCallback is unsupported."); #endif } absl::StatusOr<uint64_t> PrepareAndExecuteLoadedHostCallback( ClientHostBufferStore* host_buffer_store, xla::ifrt::LoadedHostCallback* loaded_host_callback, uint64_t operand_handle) { TF_ASSIGN_OR_RETURN(absl::Cord operands, host_buffer_store->Lookup(operand_handle).Await()); absl::Cleanup cleanup = [&]() { host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); }; TF_ASSIGN_OR_RETURN( absl::Cord results, ExecuteLoadedHostCallback(loaded_host_callback, std::move(operands))); const uint64_t result_handle = host_buffer_store->NextHandle(); TF_RETURN_IF_ERROR(host_buffer_store->Store(result_handle, results).Await()); return result_handle; } absl::Cleanup cleanup = [&]() { host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); }; host_buffer_store->Delete(operand_handle).OnReady([](absl::Status status) { if (!status.ok()) { LOG(ERROR) << "Failed to delete host callback operands: " << status; } }); LoadedExecutable::LoadedExecutable( xla::ifrt::Client* client, std::shared_ptr<RpcHelper> rpc_helper, uint64_t handle, std::string name, int num_devices, std::vector<xla::ifrt::LoadedExecutable::LogicalDeviceIds> addressable_device_logical_device_ids, std::vector<xla::ifrt::Device*> addressable_devices, absl::StatusOr<std::optional<std::string>> fingerprint, Future<> ready_future, std::vector<tsl::RCReference<xla::ifrt::LoadedHostCallback>> loaded_host_callbacks, std::vector<uint64_t> loaded_host_callback_handles) : client_(client), rpc_helper_(std::move(rpc_helper)), handle_(handle), name_(std::move(name)), num_devices_(num_devices), addressable_device_logical_device_ids_( std::move(addressable_device_logical_device_ids)), addressable_devices_(std::move(addressable_devices)), fingerprint_(std::move(fingerprint)), ready_future_(std::move(ready_future)) { // Start host callback pollers. CHECK_EQ(loaded_host_callbacks.size(), loaded_host_callback_handles.size()); if (!loaded_host_callbacks.empty()) { for (int i = 0; i < loaded_host_callbacks.size(); ++i) { PollLoadedHostCallback(loaded_host_callback_handles[i], loaded_host_callbacks[i]); } } // Asynchronously fetch shardings. Since users of `LoadedExecutable` typically // require sharding information to invoke the executable, it is beneficial to // eagerly schedule this fetch since, in some implementations, it may take a // long time for sharding information to be available. auto promise = Future<std::shared_ptr<Metadata>>::CreatePromise(); metadata_future_ = Future<std::shared_ptr<Metadata>>(promise); auto req = std::make_unique<LoadedExecutableMetadataRequest>(); req->set_loaded_executable_handle(handle_); auto on_done = [promise]( absl::StatusOr<std::shared_ptr<LoadedExecutableMetadataResponse>> response) mutable { if (!response.ok()) { LOG(ERROR) << "LoadedExecutableMetadata: Got " << response.status(); promise.Set(response.status()); return; } auto info = std::make_shared<Metadata>(); if (response.value()->has_parameter_shardings()) { const auto& p = response.value()->parameter_shardings().shardings(); info->parameter_shardings.emplace(p.begin(), p.end()); } if (response.value()->has_output_shardings()) { const auto& o = response.value()->output_shardings().shardings(); info->output_shardings.emplace(o.begin(), o.end()); } auto parse_layouts = [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; if (response.value()->has_parameter_layouts_list()) { info->parameter_layouts = parse_layouts(response.value()->parameter_layouts_list()); } else if (response.value()->has_parameter_layouts_error()) { info->parameter_layouts = tsl::StatusFromProto(response.value()->parameter_layouts_error()); } else { info->parameter_layouts = absl::UnimplementedError( "IFRT Proxy server did not return parameter layouts"); } if (response.value()->has_output_layouts_list()) { info->output_layouts = parse_layouts(response.value()->output_layouts_list()); } else if (response.value()->has_output_layouts_error()) { info->output_layouts = tsl::StatusFromProto(response.value()->output_layouts_error()); } else { info->output_layouts = absl::UnimplementedError( "IFRT Proxy server did not return output layouts"); } if (const absl::Status s = tsl::StatusFromProto( response.value()->output_memory_kinds().status()); !s.ok()) { info->output_memory_kinds = s; } else { std::vector<std::vector<absl::string_view>> output_memory_kinds; for (const auto& list : response.value()->output_memory_kinds().memory_kind_lists()) { std::vector<absl::string_view> kinds; kinds.reserve(list.memory_kinds_size()); for (const absl::string_view kind : list.memory_kinds()) { const auto it = info->memory_kinds.insert(std::string(kind)).first; kinds.push_back(*it); } output_memory_kinds.push_back(std::move(kinds)); } info->output_memory_kinds = std::move(output_memory_kinds); } promise.Set(std::move(info)); }; rpc_helper_->LoadedExecutableMetadata(std::move(req)) .OnReady(std::move(on_done)); } [promise]( absl::StatusOr<std::shared_ptr<LoadedExecutableMetadataResponse>> response) mutable { if (!response.ok()) { LOG(ERROR) << "LoadedExecutableMetadata: Got " << response.status(); promise.Set(response.status()); return; } auto info = std::make_shared<Metadata>(); if (response.value()->has_parameter_shardings()) { const auto& p = response.value()->parameter_shardings().shardings(); info->parameter_shardings.emplace(p.begin(), p.end()); } if (response.value()->has_output_shardings()) { const auto& o = response.value()->output_shardings().shardings(); info->output_shardings.emplace(o.begin(), o.end()); } auto parse_layouts = [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; if (response.value()->has_parameter_layouts_list()) { info->parameter_layouts = parse_layouts(response.value()->parameter_layouts_list()); } else if (response.value()->has_parameter_layouts_error()) { info->parameter_layouts = tsl::StatusFromProto(response.value()->parameter_layouts_error()); } else { info->parameter_layouts = absl::UnimplementedError( "IFRT Proxy server did not return parameter layouts"); } if (response.value()->has_output_layouts_list()) { info->output_layouts = parse_layouts(response.value()->output_layouts_list()); } else if (response.value()->has_output_layouts_error()) { info->output_layouts = tsl::StatusFromProto(response.value()->output_layouts_error()); } else { info->output_layouts = absl::UnimplementedError( "IFRT Proxy server did not return output layouts"); } if (const absl::Status s = tsl::StatusFromProto( response.value()->output_memory_kinds().status()); !s.ok()) { info->output_memory_kinds = s; } else { std::vector<std::vector<absl::string_view>> output_memory_kinds; for (const auto& list : response.value()->output_memory_kinds().memory_kind_lists()) { std::vector<absl::string_view> kinds; kinds.reserve(list.memory_kinds_size()); for (const absl::string_view kind : list.memory_kinds()) { const auto it = info->memory_kinds.insert(std::string(kind)).first; kinds.push_back(*it); } output_memory_kinds.push_back(std::move(kinds)); } info->output_memory_kinds = std::move(output_memory_kinds); } promise.Set(std::move(info)); }; [](const LoadedExecutableMetadataResponse::LayoutList& list) { std::vector<xla::Layout> layouts; layouts.reserve(list.layouts_size()); for (const auto& layout : list.layouts()) { layouts.push_back(xla::Layout::CreateFromProto(layout)); } return layouts; }; LoadedExecutable::~LoadedExecutable() { auto req = std::make_unique<LoadedExecutableDestructRequest>(); req->set_loaded_executable_handle(handle_); rpc_helper_->LoadedExecutableDestruct(std::move(req)) .OnReady( [](absl::StatusOr<std::shared_ptr<LoadedExecutableDestructResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to destroy `LoadedExecutable`: " << response.status(); } }); } [](absl::StatusOr<std::shared_ptr<LoadedExecutableDestructResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to destroy `LoadedExecutable`: " << response.status(); } }); xla::ifrt::Client* LoadedExecutable::client() const { return client_; } absl::string_view LoadedExecutable::name() const { return name_; } absl::StatusOr<std::optional<std::string>> LoadedExecutable::Fingerprint() const { return fingerprint_; } absl::StatusOr<std::string> LoadedExecutable::Serialize() const { return absl::UnimplementedError( "IFRT service executable does not support `Serialize` since the " "underlying serialization format is not stable"); } Future<> LoadedExecutable::GetReadyFuture() const { return ready_future_; } int LoadedExecutable::num_devices() const { return num_devices_; } int64_t LoadedExecutable::SizeOfGeneratedCodeInBytes() const { LOG(FATAL) << "Unimplemented"; } absl::StatusOr<CompiledMemoryStats> LoadedExecutable::GetCompiledMemoryStats() const { return absl::UnimplementedError("Unimplemented"); } std::optional<std::vector<OpSharding>> LoadedExecutable::GetParameterShardings() const { auto info = metadata_future_.Await(); if (!info.ok()) { return std::nullopt; } return (*info)->parameter_shardings; } std::optional<std::vector<OpSharding>> LoadedExecutable::GetOutputShardings() const { auto info = metadata_future_.Await(); if (!info.ok()) { return std::nullopt; } return (*info)->output_shardings; } LoadedExecutable::GetParameterLayouts() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); TF_RETURN_IF_ERROR(info->parameter_layouts.status()); std::vector<std::unique_ptr<Layout>> result; result.reserve(info->parameter_layouts->size()); for (const xla::Layout& layout : *info->parameter_layouts) { result.push_back(std::make_unique<xla::PjRtXlaLayout>(layout)); } return result; } LoadedExecutable::GetOutputLayouts() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); TF_RETURN_IF_ERROR(info->output_layouts.status()); std::vector<std::unique_ptr<Layout>> result; result.reserve(info->output_layouts->size()); for (const xla::Layout& layout : *info->output_layouts) { result.push_back(std::make_unique<xla::PjRtXlaLayout>(layout)); } return result; } LoadedExecutable::GetOutputMemoryKinds() const { TF_ASSIGN_OR_RETURN(auto info, metadata_future_.Await()); return info->output_memory_kinds; } LoadedExecutable::GetHloModules() const { return absl::UnimplementedError( "IFRT service does not support LoadedExecutable::GetHloModules() since " "HloModule does not provide stable serialization"); } LoadedExecutable::GetCostAnalysis() const { return absl::UnimplementedError("Unimplemented"); } LoadedExecutable::Execute(absl::Span<tsl::RCReference<xla::ifrt::Array>> args, const ExecuteOptions& options, std::optional<xla::ifrt::DeviceList> devices) { auto req = std::make_unique<LoadedExecutableExecuteRequest>(); req->set_loaded_executable_handle(handle_); for (const auto& arg : args) { auto* array = llvm::dyn_cast_or_null<Array>(arg.get()); if (array == nullptr) { return absl::InvalidArgumentError( "Invalid IFRT array type provided to `LoadedExecutable::Execute`"); } req->add_args_handles(array->handle().handle); } TF_ASSIGN_OR_RETURN(*req->mutable_execute_options(), options.ToProto()); if (devices.has_value()) { for (const auto* device : *devices) { req->add_device_ids(device->Id().value()); } } TF_ASSIGN_OR_RETURN( std::shared_ptr<LoadedExecutableExecuteResponse> response, rpc_helper_->LoadedExecutableExecute(std::move(req)).Await()); // NOTE: All future and array handles in `response` must have an owner // locally, or be requested to be destructed remotely, before returning. xla::ifrt::LoadedExecutable::ExecuteResult result; // Populate the execution status future. `CheckFuture` deletes the server-side // futures after its completion. result.status = rpc_helper_->CheckFuture(response->status_handle()); // Create output arrays. The cleanup logic ensures that all handles are // properly cleaned up on early return. absl::Cleanup cleanup = [&]() { int index = result.outputs.size(); result.outputs.clear(); // Cleaned up by `~Array()`. for (; index < response->outputs_size(); ++index) { Array::Destruct(rpc_helper_.get(), ArrayHandle{response->outputs(index).array_handle()}); } }; const auto lookup_device = absl::bind_front(&Client::LookupDevice, client()); for (const auto& output : response->outputs()) { TF_ASSIGN_OR_RETURN(DType dtype, DType::FromProto(output.dtype())); TF_ASSIGN_OR_RETURN(Shape shape, Shape::FromProto(output.shape())); TF_ASSIGN_OR_RETURN(auto sharding, Sharding::FromProto(lookup_device, output.sharding())); result.outputs.push_back(tsl::MakeRef<Array>( client(), rpc_helper_, dtype, std::move(shape), std::move(sharding), ArrayHandle{output.array_handle()})); } std::move(cleanup).Cancel(); return result; } absl::Cleanup cleanup = [&]() { int index = result.outputs.size(); result.outputs.clear(); // Cleaned up by `~Array()`. for (; index < response->outputs_size(); ++index) { Array::Destruct(rpc_helper_.get(), ArrayHandle{response->outputs(index).array_handle()}); } }; Future<> LoadedExecutable::Delete() { auto req = std::make_unique<LoadedExecutableDeleteRequest>(); req->set_loaded_executable_handle(handle_); absl::StatusOr<std::shared_ptr<LoadedExecutableDeleteResponse>> response = rpc_helper_->LoadedExecutableDelete(std::move(req)).Await(); if (!response.ok()) { return Future<>(response.status()); } return rpc_helper_->CheckFuture((*response)->future_handle()); } bool LoadedExecutable::IsDeleted() const { auto req = std::make_unique<LoadedExecutableIsDeletedRequest>(); req->set_loaded_executable_handle(handle_); absl::StatusOr<std::shared_ptr<LoadedExecutableIsDeletedResponse>> response = rpc_helper_->LoadedExecutableIsDeleted(std::move(req)).Await(); if (!response.ok()) { LOG(ERROR) << "Failed to query the deletion status of `LoadedExecutable`: " << response.status(); return false; } return (*response)->is_deleted(); } LoadedExecutable::addressable_device_logical_ids() const { return addressable_device_logical_device_ids_; } absl::Span<xla::ifrt::Device* const> LoadedExecutable::addressable_devices() const { return addressable_devices_; } void LoadedExecutable::PollLoadedHostCallback( uint64_t handle, tsl::RCReference<xla::ifrt::LoadedHostCallback> loaded_host_callback) { // Note: individual host callbacks may live longer than the executable as the // destruction of an IFRT executable is not required to block until all // in-flight executions are complete. Therefore, the following lambda must not // capture `this` and is scheduled on the default thread pool. auto f = [rpc_helper = rpc_helper_, handle, loaded_host_callback = std::move(loaded_host_callback)]() { while (true) { const uint64_t operand_handle = rpc_helper->host_buffer_store()->NextHandle(); auto poll_req = std::make_unique<LoadedHostCallbackPollRequest>(); poll_req->set_loaded_host_callback_handle(handle); poll_req->set_operand_host_buffer_handle(operand_handle); auto response = rpc_helper->LoadedHostCallbackPoll(std::move(poll_req)).Await(); if (!response.ok()) { LOG_EVERY_N_SEC(ERROR, 60) << "Failed to poll host callback execution: " << response.status(); continue; } if (!(*response)->has_host_callback_execution_handle()) { // The host callback is destructed from the server. break; } auto ret_req = std::make_unique<LoadedHostCallbackReturnRequest>(); ret_req->set_host_callback_execution_handle( (*response)->host_callback_execution_handle()); absl::StatusOr<uint64_t> result_handle = PrepareAndExecuteLoadedHostCallback( rpc_helper->host_buffer_store().get(), loaded_host_callback.get(), operand_handle); if (result_handle.ok()) { ret_req->set_result_host_buffer_handle(*result_handle); } else { *ret_req->mutable_error() = tsl::StatusToProto(result_handle.status()); } rpc_helper->LoadedHostCallbackReturn(std::move(ret_req)) .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } }); } }; static auto* global_pool = new tsl::thread::ThreadPool( tsl::Env::Default(), GetThreadOptions(), "XLAIFRTProxy", std::min(16, tsl::port::MaxParallelism())); global_pool->Schedule(std::move(f)); } auto f = [rpc_helper = rpc_helper_, handle, loaded_host_callback = std::move(loaded_host_callback)]() { while (true) { const uint64_t operand_handle = rpc_helper->host_buffer_store()->NextHandle(); auto poll_req = std::make_unique<LoadedHostCallbackPollRequest>(); poll_req->set_loaded_host_callback_handle(handle); poll_req->set_operand_host_buffer_handle(operand_handle); auto response = rpc_helper->LoadedHostCallbackPoll(std::move(poll_req)).Await(); if (!response.ok()) { LOG_EVERY_N_SEC(ERROR, 60) << "Failed to poll host callback execution: " << response.status(); continue; } if (!(*response)->has_host_callback_execution_handle()) { // The host callback is destructed from the server. break; } auto ret_req = std::make_unique<LoadedHostCallbackReturnRequest>(); ret_req->set_host_callback_execution_handle( (*response)->host_callback_execution_handle()); absl::StatusOr<uint64_t> result_handle = PrepareAndExecuteLoadedHostCallback( rpc_helper->host_buffer_store().get(), loaded_host_callback.get(), operand_handle); if (result_handle.ok()) { ret_req->set_result_host_buffer_handle(*result_handle); } else { *ret_req->mutable_error() = tsl::StatusToProto(result_handle.status()); } rpc_helper->LoadedHostCallbackReturn(std::move(ret_req)) .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } }); } }; .OnReady([](absl::StatusOr< std::shared_ptr<LoadedHostCallbackReturnResponse>> response) { if (!response.ok()) { LOG(ERROR) << "Failed to return host callback results: " << response.status(); } });
#include "xla/python/ifrt_proxy/client/executable.h" #include <cstdint> #include <memory> #include <vector> #include <gmock/gmock.h> #include <gtest/gtest.h> #include "absl/status/status.h" #include "absl/types/span.h" #include "llvm/Support/Casting.h" #include "xla/layout_util.h" #include "xla/pjrt/pjrt_common.h" #include "xla/pjrt/pjrt_layout.h" #include "xla/python/ifrt/array.h" #include "xla/python/ifrt/device.h" #include "xla/python/ifrt/dtype.h" #include "xla/python/ifrt/executable.h" #include "xla/python/ifrt/future.h" #include "xla/python/ifrt/memory.h" #include "xla/python/ifrt/mock.h" #include "xla/python/ifrt/shape.h" #include "xla/python/ifrt/sharding.h" #include "xla/python/ifrt_proxy/client/array.h" #include "xla/python/ifrt_proxy/client/client_session.h" #include "xla/python/ifrt_proxy/client/host_buffer.h" #include "xla/python/ifrt_proxy/client/mock_client_session.h" #include "xla/python/ifrt_proxy/client/mock_host_buffer.h" #include "xla/python/ifrt_proxy/client/rpc_helper.h" #include "xla/python/ifrt_proxy/client/version.h" #include "xla/python/ifrt_proxy/common/ifrt_service.pb.h" #include "xla/python/ifrt_proxy/common/types.h" #include "xla/tsl/concurrency/ref_count.h" #include "tsl/platform/casts.h" #include "tsl/platform/protobuf.h" // IWYU pragma: keep #include "tsl/platform/status_matchers.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test.h" class LoadedExecutableTest : public ::testing::Test { protected: void SetUp() override { session_ = std::make_shared<MockClientSession>(); rpc_helper_ = std::make_shared<RpcHelper>(Version(), session_); host_buffer_store_ = std::make_shared<MockClientHostBufferStore>(); rpc_helper_->set_host_buffer_store(host_buffer_store_); // Default handler that ignores all uninteresting requests, but still // invokes the callback in order to avoid hanging the caller forever. EXPECT_CALL(*session_, Enqueue(_)) .WillRepeatedly(Return(Future<ClientSession::Response>( absl::InternalError("Request has no mock handlers")))); } std::shared_ptr<MockClientSession> session_; std::shared_ptr<RpcHelper> rpc_helper_; std::shared_ptr<ClientHostBufferStore> host_buffer_store_; };
ReferenceUtilTest_MapArray2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, MapArray2D) { auto identity = [](float value) { return std::log(std::exp(value)); }; auto result = ReferenceUtil::MapArray2D(*matrix_, identity); auto actual_literal = LiteralUtil::CreateR2FromArray2D(*result); LiteralTestUtil::ExpectR2NearArray2D(*matrix_, actual_literal, ErrorSpec(0.0001)); }
PreemptionSyncManagerTest_PreemptFastTask
xla/tsl/distributed_runtime/preemption/preemption_sync_manager_test.cc
PreemptionSyncManagerImpl() = default; ~PreemptionSyncManagerImpl() override { shutdown_.Notify(); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent) { return Initialize(agent, "sigterm"); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent, const std::string& preemption_notifier_type) { TF_ASSIGN_OR_RETURN(Env * env, agent->GetEnv()); return Initialize(agent, PreemptionNotifier::CreatePreemptionNotifier( preemption_notifier_type, env)); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent, std::unique_ptr<PreemptionNotifier> notifier) { TF_ASSIGN_OR_RETURN(Env * env, agent->GetEnv()); env_ = env; agent_ = agent; preemption_notifier_ = std::move(notifier); TF_ASSIGN_OR_RETURN(CoordinatedTask own_task, agent->GetOwnTask()); const std::string task_name = absl::StrCat("/job:", own_task.job_name(), "/task:", own_task.task_id()); current_call_counter_key_ = absl::StrCat(kPreemptionCounterDirKey, task_name); /* Listen for preemption notice within this task, then notify coordination * service when death time is within kProtocolDuration. */ preemption_notifier_->WillBePreemptedAtAsync( [agent = agent_, task_name](absl::StatusOr<absl::Time> death_time) { if (!death_time.ok()) { // The preemption notifier invokes callback with Cancelled error when // its being destructed. if (errors::IsCancelled(death_time.status())) { LOG(INFO) << "Preemption sync protocol cancelled by notifier: " << death_time.status() << ". This is expected during program shutdown."; } else { LOG(ERROR) << "Error from preemption notifier: " << death_time.status(); } return; } notified_metric->GetCell()->Set(true); // Notify coordination service about preemption notice. const absl::Status s = agent->InsertKeyValue( kPreemptionNoticeKey, absl::FormatTime(*death_time)); LOG(INFO) << "Notified coordination service that this task will " "be preempted at " << *death_time << ". absl::Status: " << s; }); /* Listen for preemption notice (death time) from coordination service, which * triggers the sync protocol. */ call_opts_ = agent_->GetKeyValueAsync( kPreemptionNoticeKey, [this, agent = agent_](absl::StatusOr<std::string> status_or_death_time) { if (errors::IsCancelled(status_or_death_time.status())) { // The agent cancels pending GetKeyValue RPCs because of shutdown, // so simply log and return. LOG(INFO) << "Cancelled call to retrieve preemption notice. This is " "expected upon program shutdown."; return; } else if (!status_or_death_time.ok()) { LOG(WARNING) << "Failed to retrieve preemption notice from " "coordination service: " << status_or_death_time.status() << ". This is only expected if one of the tasks is unhealthy." " Check the logs for the actual root cause."; // Notify other tasks to not wait at the barrier. Note: // CancelPreemptionBarrier() cannot be used because this may be // triggered after preemption sync manager has been destroyed. agent->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); return; } std::string err; absl::Time death_time; if (absl::ParseTime(absl::RFC3339_full, *status_or_death_time, &death_time, &err)) { LOG(INFO) << "Received preemption notice with death_time " << death_time; } else { LOG(ERROR) << "Unable to parse preemption notice's death time: " << err; CancelPreemptionBarrier(); return; } // Trigger protocol in a separate thread: compute max call counter. sync_protocol_thread_ = absl::WrapUnique(env_->StartThread( {}, "PreemptionSyncManager_SyncProtocol", std::bind(&PreemptionSyncManagerImpl::ComputeSyncCallCounter, this, death_time))); }); return absl::OkStatus(); } [agent = agent_, task_name](absl::StatusOr<absl::Time> death_time) { if (!death_time.ok()) { // The preemption notifier invokes callback with Cancelled error when // its being destructed. if (errors::IsCancelled(death_time.status())) { LOG(INFO) << "Preemption sync protocol cancelled by notifier: " << death_time.status() << ". This is expected during program shutdown."; } else { LOG(ERROR) << "Error from preemption notifier: " << death_time.status(); } return; } notified_metric->GetCell()->Set(true); // Notify coordination service about preemption notice. const absl::Status s = agent->InsertKeyValue( kPreemptionNoticeKey, absl::FormatTime(*death_time)); LOG(INFO) << "Notified coordination service that this task will " "be preempted at " << *death_time << ". absl::Status: " << s; }); [this, agent = agent_](absl::StatusOr<std::string> status_or_death_time) { if (errors::IsCancelled(status_or_death_time.status())) { // The agent cancels pending GetKeyValue RPCs because of shutdown, // so simply log and return. LOG(INFO) << "Cancelled call to retrieve preemption notice. This is " "expected upon program shutdown."; return; } else if (!status_or_death_time.ok()) { LOG(WARNING) << "Failed to retrieve preemption notice from " "coordination service: " << status_or_death_time.status() << ". This is only expected if one of the tasks is unhealthy." " Check the logs for the actual root cause."; // Notify other tasks to not wait at the barrier. Note: // CancelPreemptionBarrier() cannot be used because this may be // triggered after preemption sync manager has been destroyed. agent->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); return; } std::string err; absl::Time death_time; if (absl::ParseTime(absl::RFC3339_full, *status_or_death_time, &death_time, &err)) { LOG(INFO) << "Received preemption notice with death_time " << death_time; } else { LOG(ERROR) << "Unable to parse preemption notice's death time: " << err; CancelPreemptionBarrier(); return; } // Trigger protocol in a separate thread: compute max call counter. sync_protocol_thread_ = absl::WrapUnique(env_->StartThread( {}, "PreemptionSyncManager_SyncProtocol", std::bind(&PreemptionSyncManagerImpl::ComputeSyncCallCounter, this, death_time))); }); kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); void PreemptionSyncManagerImpl::ComputeSyncCallCounter(absl::Time death_time) { // 1. If death time is in the distant future, sleep until there's // `kProtocolDuration` left until death time before we begin the protocol. const absl::Duration remaining_time = death_time - absl::Now(); if (remaining_time > kProtocolDuration) { LOG(INFO) << "Will begin preemption sync protocol in " << remaining_time; const absl::Duration sleep_time = remaining_time - kProtocolDuration; if (shutdown_.WaitForNotificationWithTimeout(sleep_time)) { // If shutdown is triggered midway, exit thread immediately. LOG(WARNING) << "Shutdown is triggered before preemption sync protocol has begun."; CancelPreemptionBarrier(); return; } } // 2. Send coordination service the task's current call counter. Hold the lock // to prevent updates to `call_counter_` until the protocol completes and this // function exits, implying that we have decided on a new // `preemption_sync_counter_` or the protocol failed. This ensures correctness // of the preemption sync protocol. mutex_lock l(mu_); const absl::Status notified_status = agent_->InsertKeyValue( current_call_counter_key_, std::to_string(call_counter_)); if (!notified_status.ok()) { LOG(ERROR) << "Preemption sync failed - could not inform service of " "current call counter: " << notified_status; CancelPreemptionBarrier(); return; } // 3. Impose a barrier to wait until everybody sends their current call // counter. const absl::Status barrier_status = agent_->WaitAtBarrier(kPreemptionBarrier, kPreemptionBarrierTimeout, {}); if (!barrier_status.ok()) { LOG(ERROR) << "Preemption sync barrier failed: " << barrier_status; return; } // 4. Retrieve every task's current call counter. absl::StatusOr<std::vector<KeyValueEntry>> all_counters = agent_->GetKeyValueDir(kPreemptionCounterDirKey); if (!all_counters.ok()) { LOG(ERROR) << "Preemption sync failed - unable to retrieve call counters: " << all_counters.status(); return; } // 5. Compute the fastest task's call counter. // Note: Each task should retrieve the same set of call counters and arrive at // the same maximum. We have to calculate this max within each task because // coordination service does not provide GetMaxKeyValue(). int64_t max_counter = kPreemptionSyncUnsetCounter; for (const auto& kv : *all_counters) { int64_t call_counter; if (!absl::SimpleAtoi(kv.value(), &call_counter)) { LOG(ERROR) << "Preemption sync failed - failed to parse preemption call " "counter: " << kv.DebugString(); return; } max_counter = std::max(max_counter, call_counter); } if (max_counter == kPreemptionSyncUnsetCounter) { LOG(ERROR) << "Preemption sync failed - no call counters found."; return; } // 6. Set sync point to be the next possible call counter of the fastest task. preemption_sync_counter_ = max_counter + 1; LOG(INFO) << "Preemption sync counter is set: " << preemption_sync_counter_; set_sync_point_metric->GetCell()->Set(true); } void PreemptionSyncManagerImpl::CancelPreemptionBarrier() { agent_->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); } kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); bool PreemptionSyncManagerImpl::ReachedSyncPoint(int step_counter) { // Record that this API was called at least once. sync_usage_metric->GetCell()->Set(true); // Note: if a preemption notice has been received and ComputeSyncCallCounter() // is ongoing , this method will be blocked until it acquires the lock. This // prevents updates to `call_counter_` while `preemption_sync_counter_` is // being computed, which ensures correctness of the preemption sync protocol. mutex_lock l(mu_); // Track current call. call_counter_ = step_counter; VLOG(3) << "Current call counter: " << call_counter_ << ", Preemption sync point: " << preemption_sync_counter_; const bool reached_sync_point = preemption_sync_counter_ == call_counter_; if (reached_sync_point) { // Record that this job reached the sync point. reached_sync_point_metric->GetCell()->Set(true); } return reached_sync_point; } VLOG(3) << "Current call counter: " << call_counter_ std::unique_ptr<PreemptionSyncManager> CreatePreemptionSyncManager() { return std::make_unique<PreemptionSyncManagerImpl>(); }
#include "xla/tsl/distributed_runtime/preemption/preemption_sync_manager.h" #include <memory> #include <string> #include <utility> #include "grpcpp/server.h" #include "grpcpp/server_builder.h" #include "grpcpp/support/channel_arguments.h" #include "absl/memory/memory.h" #include "absl/time/clock.h" #include "absl/time/time.h" #include "xla/tsl/distributed_runtime/coordination/coordination_client.h" #include "xla/tsl/distributed_runtime/coordination/coordination_service.h" #include "xla/tsl/distributed_runtime/coordination/coordination_service_agent.h" #include "xla/tsl/distributed_runtime/preemption/preemption_notifier.h" #include "xla/tsl/distributed_runtime/rpc/async_service_interface.h" #include "xla/tsl/distributed_runtime/rpc/coordination/grpc_coordination_client.h" #include "xla/tsl/distributed_runtime/rpc/coordination/grpc_coordination_service_impl.h" #include "tsl/platform/env.h" #include "tsl/platform/errors.h" #include "tsl/platform/status.h" #include "tsl/platform/test.h" #include "tsl/platform/threadpool.h" #include "tsl/protobuf/coordination_config.pb.h" class PreemptionSyncManagerTest : public ::testing::Test { protected: PreemptionSyncManagerTest() { // Setup coordination service. StartCoordinationService(); InitializeAndConnectCoordinationAgents(); // Create preempt sync manager for task 1. auto preempt_notifier = std::make_unique<FakePreemptionNotifier>(); preempt_notifier_ = preempt_notifier.get(); TF_CHECK_OK(preempt_sync_mgr_->Initialize(coord_agent_.get(), std::move(preempt_notifier))); // Create preempt sync manager for task 2. auto preempt_notifier2 = std::make_unique<FakePreemptionNotifier>(); preempt_notifier2_ = preempt_notifier2.get(); TF_CHECK_OK(preempt_sync_mgr2_->Initialize(coord_agent2_.get(), std::move(preempt_notifier2))); } ~PreemptionSyncManagerTest() override { // Tear down coordination service objects in order. preempt_sync_mgr_ = nullptr; preempt_sync_mgr2_ = nullptr; coord_agent_ = nullptr; coord_agent2_ = nullptr; coord_service_ = nullptr; static_cast<tsl::GrpcCoordinationServiceImpl*>(coord_rpc_service_.get()) ->SetCoordinationServiceInstance(nullptr); grpc_server_->Shutdown(); coord_rpc_service_->Shutdown(); } // `to_task1` toggles which of the two tasks receives preemption notice. void SendPreemptionNotice(absl::Time death_time = absl::Now(), bool to_task1 = true) { if (to_task1) { preempt_notifier_->AnnounceDeath(death_time); } else { preempt_notifier2_->AnnounceDeath(death_time); } // Block main thread for a short while to allow preemption sync manager to // process the notice. Env::Default()->SleepForMicroseconds( absl::ToInt64Microseconds(absl::Seconds(1))); } // Report to coordiation service that task two is unhealthy. void SimulateUnhealthyTaskTwo() { CoordinatedTask task2; task2.set_job_name(kJobName); task2.set_task_id(1); TF_CHECK_OK( coord_service_->ReportTaskError(task2, errors::Internal("test_error"))); } // Allow access to objects under test. std::unique_ptr<PreemptionSyncManager> preempt_sync_mgr_ = CreatePreemptionSyncManager(); std::unique_ptr<PreemptionSyncManager> preempt_sync_mgr2_ = CreatePreemptionSyncManager(); protected: // Utility methods to set up coordination service and agents. void StartCoordinationService() { ::grpc::ServerBuilder builder; coord_service_ = EnableCoordinationService(); coord_compute_pool_ = std::make_unique<thread::ThreadPool>( Env::Default(), "CoordinationServiceRpcHandler", /*num_threads=*/1); coord_rpc_service_ = std::make_unique<GrpcCoordinationServiceImpl>( coord_compute_pool_.get(), &builder); auto* grpc_coord_service = static_cast<GrpcCoordinationServiceImpl*>(coord_rpc_service_.get()); grpc_coord_service->SetCoordinationServiceInstance(coord_service_.get()); grpc_server_ = builder.BuildAndStart(); coord_rpc_thread_ = absl::WrapUnique(Env::Default()->StartThread( /*thread_options=*/{}, /*name=*/"CoordinationServiceHandleRPCsLoop", [service = coord_rpc_service_.get()]() { service->HandleRPCsLoop(); })); } std::unique_ptr<CoordinationServiceInterface> EnableCoordinationService() { CoordinationServiceConfig config; config.set_service_type("standalone"); CoordinatedJob* job = config.mutable_coordinated_job_list()->Add(); job->set_name(kJobName); job->set_num_tasks(2); return CoordinationServiceInterface::EnableCoordinationService( Env::Default(), config, /*cache=*/nullptr); } void InitializeAndConnectCoordinationAgents() { std::unique_ptr<CoordinationClient> coord_client = absl::WrapUnique(NewGrpcCoordinationClient( grpc_server_->InProcessChannel(::grpc::ChannelArguments()))); std::unique_ptr<CoordinationClient> coord_client2 = absl::WrapUnique(NewGrpcCoordinationClient( grpc_server_->InProcessChannel(::grpc::ChannelArguments()))); auto error_fn = [](const absl::Status& status) { LOG(ERROR) << "Coordination service agent in error status: " << status; }; TEST_F(PreemptionSyncManagerTest, PreemptFastTask) { int step_counter0 = 0; int step_counter2 = 0; // Simulate slow task 1 that is only at call #1. EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter0++)); // Simulate fast task 3 that is already at call #3. EXPECT_FALSE(preempt_sync_mgr2_->ReachedSyncPoint(step_counter2++)); EXPECT_FALSE(preempt_sync_mgr2_->ReachedSyncPoint(step_counter2++)); EXPECT_FALSE(preempt_sync_mgr2_->ReachedSyncPoint(step_counter2++)); SendPreemptionNotice(absl::Now(), /*=to_task1=*/false); // Sync point should be set at call #4. EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter0++)); EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter0++)); EXPECT_TRUE(preempt_sync_mgr_->ReachedSyncPoint(step_counter0++)); // Task 2 was already at call #3, so the next call should be the sync point. EXPECT_TRUE(preempt_sync_mgr2_->ReachedSyncPoint(step_counter2++)); }
PreemptionSyncManagerTest_PreemptSlowTask
xla/tsl/distributed_runtime/preemption/preemption_sync_manager_test.cc
PreemptionSyncManagerImpl() = default; ~PreemptionSyncManagerImpl() override { shutdown_.Notify(); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent) { return Initialize(agent, "sigterm"); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent, const std::string& preemption_notifier_type) { TF_ASSIGN_OR_RETURN(Env * env, agent->GetEnv()); return Initialize(agent, PreemptionNotifier::CreatePreemptionNotifier( preemption_notifier_type, env)); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent, std::unique_ptr<PreemptionNotifier> notifier) { TF_ASSIGN_OR_RETURN(Env * env, agent->GetEnv()); env_ = env; agent_ = agent; preemption_notifier_ = std::move(notifier); TF_ASSIGN_OR_RETURN(CoordinatedTask own_task, agent->GetOwnTask()); const std::string task_name = absl::StrCat("/job:", own_task.job_name(), "/task:", own_task.task_id()); current_call_counter_key_ = absl::StrCat(kPreemptionCounterDirKey, task_name); /* Listen for preemption notice within this task, then notify coordination * service when death time is within kProtocolDuration. */ preemption_notifier_->WillBePreemptedAtAsync( [agent = agent_, task_name](absl::StatusOr<absl::Time> death_time) { if (!death_time.ok()) { // The preemption notifier invokes callback with Cancelled error when // its being destructed. if (errors::IsCancelled(death_time.status())) { LOG(INFO) << "Preemption sync protocol cancelled by notifier: " << death_time.status() << ". This is expected during program shutdown."; } else { LOG(ERROR) << "Error from preemption notifier: " << death_time.status(); } return; } notified_metric->GetCell()->Set(true); // Notify coordination service about preemption notice. const absl::Status s = agent->InsertKeyValue( kPreemptionNoticeKey, absl::FormatTime(*death_time)); LOG(INFO) << "Notified coordination service that this task will " "be preempted at " << *death_time << ". absl::Status: " << s; }); /* Listen for preemption notice (death time) from coordination service, which * triggers the sync protocol. */ call_opts_ = agent_->GetKeyValueAsync( kPreemptionNoticeKey, [this, agent = agent_](absl::StatusOr<std::string> status_or_death_time) { if (errors::IsCancelled(status_or_death_time.status())) { // The agent cancels pending GetKeyValue RPCs because of shutdown, // so simply log and return. LOG(INFO) << "Cancelled call to retrieve preemption notice. This is " "expected upon program shutdown."; return; } else if (!status_or_death_time.ok()) { LOG(WARNING) << "Failed to retrieve preemption notice from " "coordination service: " << status_or_death_time.status() << ". This is only expected if one of the tasks is unhealthy." " Check the logs for the actual root cause."; // Notify other tasks to not wait at the barrier. Note: // CancelPreemptionBarrier() cannot be used because this may be // triggered after preemption sync manager has been destroyed. agent->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); return; } std::string err; absl::Time death_time; if (absl::ParseTime(absl::RFC3339_full, *status_or_death_time, &death_time, &err)) { LOG(INFO) << "Received preemption notice with death_time " << death_time; } else { LOG(ERROR) << "Unable to parse preemption notice's death time: " << err; CancelPreemptionBarrier(); return; } // Trigger protocol in a separate thread: compute max call counter. sync_protocol_thread_ = absl::WrapUnique(env_->StartThread( {}, "PreemptionSyncManager_SyncProtocol", std::bind(&PreemptionSyncManagerImpl::ComputeSyncCallCounter, this, death_time))); }); return absl::OkStatus(); } [agent = agent_, task_name](absl::StatusOr<absl::Time> death_time) { if (!death_time.ok()) { // The preemption notifier invokes callback with Cancelled error when // its being destructed. if (errors::IsCancelled(death_time.status())) { LOG(INFO) << "Preemption sync protocol cancelled by notifier: " << death_time.status() << ". This is expected during program shutdown."; } else { LOG(ERROR) << "Error from preemption notifier: " << death_time.status(); } return; } notified_metric->GetCell()->Set(true); // Notify coordination service about preemption notice. const absl::Status s = agent->InsertKeyValue( kPreemptionNoticeKey, absl::FormatTime(*death_time)); LOG(INFO) << "Notified coordination service that this task will " "be preempted at " << *death_time << ". absl::Status: " << s; }); [this, agent = agent_](absl::StatusOr<std::string> status_or_death_time) { if (errors::IsCancelled(status_or_death_time.status())) { // The agent cancels pending GetKeyValue RPCs because of shutdown, // so simply log and return. LOG(INFO) << "Cancelled call to retrieve preemption notice. This is " "expected upon program shutdown."; return; } else if (!status_or_death_time.ok()) { LOG(WARNING) << "Failed to retrieve preemption notice from " "coordination service: " << status_or_death_time.status() << ". This is only expected if one of the tasks is unhealthy." " Check the logs for the actual root cause."; // Notify other tasks to not wait at the barrier. Note: // CancelPreemptionBarrier() cannot be used because this may be // triggered after preemption sync manager has been destroyed. agent->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); return; } std::string err; absl::Time death_time; if (absl::ParseTime(absl::RFC3339_full, *status_or_death_time, &death_time, &err)) { LOG(INFO) << "Received preemption notice with death_time " << death_time; } else { LOG(ERROR) << "Unable to parse preemption notice's death time: " << err; CancelPreemptionBarrier(); return; } // Trigger protocol in a separate thread: compute max call counter. sync_protocol_thread_ = absl::WrapUnique(env_->StartThread( {}, "PreemptionSyncManager_SyncProtocol", std::bind(&PreemptionSyncManagerImpl::ComputeSyncCallCounter, this, death_time))); }); kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); void PreemptionSyncManagerImpl::ComputeSyncCallCounter(absl::Time death_time) { // 1. If death time is in the distant future, sleep until there's // `kProtocolDuration` left until death time before we begin the protocol. const absl::Duration remaining_time = death_time - absl::Now(); if (remaining_time > kProtocolDuration) { LOG(INFO) << "Will begin preemption sync protocol in " << remaining_time; const absl::Duration sleep_time = remaining_time - kProtocolDuration; if (shutdown_.WaitForNotificationWithTimeout(sleep_time)) { // If shutdown is triggered midway, exit thread immediately. LOG(WARNING) << "Shutdown is triggered before preemption sync protocol has begun."; CancelPreemptionBarrier(); return; } } // 2. Send coordination service the task's current call counter. Hold the lock // to prevent updates to `call_counter_` until the protocol completes and this // function exits, implying that we have decided on a new // `preemption_sync_counter_` or the protocol failed. This ensures correctness // of the preemption sync protocol. mutex_lock l(mu_); const absl::Status notified_status = agent_->InsertKeyValue( current_call_counter_key_, std::to_string(call_counter_)); if (!notified_status.ok()) { LOG(ERROR) << "Preemption sync failed - could not inform service of " "current call counter: " << notified_status; CancelPreemptionBarrier(); return; } // 3. Impose a barrier to wait until everybody sends their current call // counter. const absl::Status barrier_status = agent_->WaitAtBarrier(kPreemptionBarrier, kPreemptionBarrierTimeout, {}); if (!barrier_status.ok()) { LOG(ERROR) << "Preemption sync barrier failed: " << barrier_status; return; } // 4. Retrieve every task's current call counter. absl::StatusOr<std::vector<KeyValueEntry>> all_counters = agent_->GetKeyValueDir(kPreemptionCounterDirKey); if (!all_counters.ok()) { LOG(ERROR) << "Preemption sync failed - unable to retrieve call counters: " << all_counters.status(); return; } // 5. Compute the fastest task's call counter. // Note: Each task should retrieve the same set of call counters and arrive at // the same maximum. We have to calculate this max within each task because // coordination service does not provide GetMaxKeyValue(). int64_t max_counter = kPreemptionSyncUnsetCounter; for (const auto& kv : *all_counters) { int64_t call_counter; if (!absl::SimpleAtoi(kv.value(), &call_counter)) { LOG(ERROR) << "Preemption sync failed - failed to parse preemption call " "counter: " << kv.DebugString(); return; } max_counter = std::max(max_counter, call_counter); } if (max_counter == kPreemptionSyncUnsetCounter) { LOG(ERROR) << "Preemption sync failed - no call counters found."; return; } // 6. Set sync point to be the next possible call counter of the fastest task. preemption_sync_counter_ = max_counter + 1; LOG(INFO) << "Preemption sync counter is set: " << preemption_sync_counter_; set_sync_point_metric->GetCell()->Set(true); } void PreemptionSyncManagerImpl::CancelPreemptionBarrier() { agent_->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); } kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); bool PreemptionSyncManagerImpl::ReachedSyncPoint(int step_counter) { // Record that this API was called at least once. sync_usage_metric->GetCell()->Set(true); // Note: if a preemption notice has been received and ComputeSyncCallCounter() // is ongoing , this method will be blocked until it acquires the lock. This // prevents updates to `call_counter_` while `preemption_sync_counter_` is // being computed, which ensures correctness of the preemption sync protocol. mutex_lock l(mu_); // Track current call. call_counter_ = step_counter; VLOG(3) << "Current call counter: " << call_counter_ << ", Preemption sync point: " << preemption_sync_counter_; const bool reached_sync_point = preemption_sync_counter_ == call_counter_; if (reached_sync_point) { // Record that this job reached the sync point. reached_sync_point_metric->GetCell()->Set(true); } return reached_sync_point; } VLOG(3) << "Current call counter: " << call_counter_ std::unique_ptr<PreemptionSyncManager> CreatePreemptionSyncManager() { return std::make_unique<PreemptionSyncManagerImpl>(); }
#include "xla/tsl/distributed_runtime/preemption/preemption_sync_manager.h" #include <memory> #include <string> #include <utility> #include "grpcpp/server.h" #include "grpcpp/server_builder.h" #include "grpcpp/support/channel_arguments.h" #include "absl/memory/memory.h" #include "absl/time/clock.h" #include "absl/time/time.h" #include "xla/tsl/distributed_runtime/coordination/coordination_client.h" #include "xla/tsl/distributed_runtime/coordination/coordination_service.h" #include "xla/tsl/distributed_runtime/coordination/coordination_service_agent.h" #include "xla/tsl/distributed_runtime/preemption/preemption_notifier.h" #include "xla/tsl/distributed_runtime/rpc/async_service_interface.h" #include "xla/tsl/distributed_runtime/rpc/coordination/grpc_coordination_client.h" #include "xla/tsl/distributed_runtime/rpc/coordination/grpc_coordination_service_impl.h" #include "tsl/platform/env.h" #include "tsl/platform/errors.h" #include "tsl/platform/status.h" #include "tsl/platform/test.h" #include "tsl/platform/threadpool.h" #include "tsl/protobuf/coordination_config.pb.h" class PreemptionSyncManagerTest : public ::testing::Test { protected: PreemptionSyncManagerTest() { // Setup coordination service. StartCoordinationService(); InitializeAndConnectCoordinationAgents(); // Create preempt sync manager for task 1. auto preempt_notifier = std::make_unique<FakePreemptionNotifier>(); preempt_notifier_ = preempt_notifier.get(); TF_CHECK_OK(preempt_sync_mgr_->Initialize(coord_agent_.get(), std::move(preempt_notifier))); // Create preempt sync manager for task 2. auto preempt_notifier2 = std::make_unique<FakePreemptionNotifier>(); preempt_notifier2_ = preempt_notifier2.get(); TF_CHECK_OK(preempt_sync_mgr2_->Initialize(coord_agent2_.get(), std::move(preempt_notifier2))); } ~PreemptionSyncManagerTest() override { // Tear down coordination service objects in order. preempt_sync_mgr_ = nullptr; preempt_sync_mgr2_ = nullptr; coord_agent_ = nullptr; coord_agent2_ = nullptr; coord_service_ = nullptr; static_cast<tsl::GrpcCoordinationServiceImpl*>(coord_rpc_service_.get()) ->SetCoordinationServiceInstance(nullptr); grpc_server_->Shutdown(); coord_rpc_service_->Shutdown(); } // `to_task1` toggles which of the two tasks receives preemption notice. void SendPreemptionNotice(absl::Time death_time = absl::Now(), bool to_task1 = true) { if (to_task1) { preempt_notifier_->AnnounceDeath(death_time); } else { preempt_notifier2_->AnnounceDeath(death_time); } // Block main thread for a short while to allow preemption sync manager to // process the notice. Env::Default()->SleepForMicroseconds( absl::ToInt64Microseconds(absl::Seconds(1))); } // Report to coordiation service that task two is unhealthy. void SimulateUnhealthyTaskTwo() { CoordinatedTask task2; task2.set_job_name(kJobName); task2.set_task_id(1); TF_CHECK_OK( coord_service_->ReportTaskError(task2, errors::Internal("test_error"))); } // Allow access to objects under test. std::unique_ptr<PreemptionSyncManager> preempt_sync_mgr_ = CreatePreemptionSyncManager(); std::unique_ptr<PreemptionSyncManager> preempt_sync_mgr2_ = CreatePreemptionSyncManager(); protected: // Utility methods to set up coordination service and agents. void StartCoordinationService() { ::grpc::ServerBuilder builder; coord_service_ = EnableCoordinationService(); coord_compute_pool_ = std::make_unique<thread::ThreadPool>( Env::Default(), "CoordinationServiceRpcHandler", /*num_threads=*/1); coord_rpc_service_ = std::make_unique<GrpcCoordinationServiceImpl>( coord_compute_pool_.get(), &builder); auto* grpc_coord_service = static_cast<GrpcCoordinationServiceImpl*>(coord_rpc_service_.get()); grpc_coord_service->SetCoordinationServiceInstance(coord_service_.get()); grpc_server_ = builder.BuildAndStart(); coord_rpc_thread_ = absl::WrapUnique(Env::Default()->StartThread( /*thread_options=*/{}, /*name=*/"CoordinationServiceHandleRPCsLoop", [service = coord_rpc_service_.get()]() { service->HandleRPCsLoop(); })); } std::unique_ptr<CoordinationServiceInterface> EnableCoordinationService() { CoordinationServiceConfig config; config.set_service_type("standalone"); CoordinatedJob* job = config.mutable_coordinated_job_list()->Add(); job->set_name(kJobName); job->set_num_tasks(2); return CoordinationServiceInterface::EnableCoordinationService( Env::Default(), config, /*cache=*/nullptr); } void InitializeAndConnectCoordinationAgents() { std::unique_ptr<CoordinationClient> coord_client = absl::WrapUnique(NewGrpcCoordinationClient( grpc_server_->InProcessChannel(::grpc::ChannelArguments()))); std::unique_ptr<CoordinationClient> coord_client2 = absl::WrapUnique(NewGrpcCoordinationClient( grpc_server_->InProcessChannel(::grpc::ChannelArguments()))); auto error_fn = [](const absl::Status& status) { LOG(ERROR) << "Coordination service agent in error status: " << status; }; TEST_F(PreemptionSyncManagerTest, PreemptSlowTask) { int step_counter0 = 0; int step_counter2 = 0; // Simulate slow task 1 that is only at call #1. EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter0++)); // Simulate fast task 3 that is already at call #3. EXPECT_FALSE(preempt_sync_mgr2_->ReachedSyncPoint(step_counter2++)); EXPECT_FALSE(preempt_sync_mgr2_->ReachedSyncPoint(step_counter2++)); EXPECT_FALSE(preempt_sync_mgr2_->ReachedSyncPoint(step_counter2++)); SendPreemptionNotice(); // Sync point should be set at call #4. EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter0++)); EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter0++)); EXPECT_TRUE(preempt_sync_mgr_->ReachedSyncPoint(step_counter0++)); // Task 2 was already at call #3, so the next call should be the sync point. EXPECT_TRUE(preempt_sync_mgr2_->ReachedSyncPoint(step_counter2++)); }
PreemptionSyncManagerTest_ShutdownTasksWithoutPreemption
xla/tsl/distributed_runtime/preemption/preemption_sync_manager_test.cc
PreemptionSyncManagerImpl() = default; ~PreemptionSyncManagerImpl() override { shutdown_.Notify(); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent) { return Initialize(agent, "sigterm"); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent, const std::string& preemption_notifier_type) { TF_ASSIGN_OR_RETURN(Env * env, agent->GetEnv()); return Initialize(agent, PreemptionNotifier::CreatePreemptionNotifier( preemption_notifier_type, env)); } absl::Status PreemptionSyncManagerImpl::Initialize( CoordinationServiceAgent* agent, std::unique_ptr<PreemptionNotifier> notifier) { TF_ASSIGN_OR_RETURN(Env * env, agent->GetEnv()); env_ = env; agent_ = agent; preemption_notifier_ = std::move(notifier); TF_ASSIGN_OR_RETURN(CoordinatedTask own_task, agent->GetOwnTask()); const std::string task_name = absl::StrCat("/job:", own_task.job_name(), "/task:", own_task.task_id()); current_call_counter_key_ = absl::StrCat(kPreemptionCounterDirKey, task_name); /* Listen for preemption notice within this task, then notify coordination * service when death time is within kProtocolDuration. */ preemption_notifier_->WillBePreemptedAtAsync( [agent = agent_, task_name](absl::StatusOr<absl::Time> death_time) { if (!death_time.ok()) { // The preemption notifier invokes callback with Cancelled error when // its being destructed. if (errors::IsCancelled(death_time.status())) { LOG(INFO) << "Preemption sync protocol cancelled by notifier: " << death_time.status() << ". This is expected during program shutdown."; } else { LOG(ERROR) << "Error from preemption notifier: " << death_time.status(); } return; } notified_metric->GetCell()->Set(true); // Notify coordination service about preemption notice. const absl::Status s = agent->InsertKeyValue( kPreemptionNoticeKey, absl::FormatTime(*death_time)); LOG(INFO) << "Notified coordination service that this task will " "be preempted at " << *death_time << ". absl::Status: " << s; }); /* Listen for preemption notice (death time) from coordination service, which * triggers the sync protocol. */ call_opts_ = agent_->GetKeyValueAsync( kPreemptionNoticeKey, [this, agent = agent_](absl::StatusOr<std::string> status_or_death_time) { if (errors::IsCancelled(status_or_death_time.status())) { // The agent cancels pending GetKeyValue RPCs because of shutdown, // so simply log and return. LOG(INFO) << "Cancelled call to retrieve preemption notice. This is " "expected upon program shutdown."; return; } else if (!status_or_death_time.ok()) { LOG(WARNING) << "Failed to retrieve preemption notice from " "coordination service: " << status_or_death_time.status() << ". This is only expected if one of the tasks is unhealthy." " Check the logs for the actual root cause."; // Notify other tasks to not wait at the barrier. Note: // CancelPreemptionBarrier() cannot be used because this may be // triggered after preemption sync manager has been destroyed. agent->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); return; } std::string err; absl::Time death_time; if (absl::ParseTime(absl::RFC3339_full, *status_or_death_time, &death_time, &err)) { LOG(INFO) << "Received preemption notice with death_time " << death_time; } else { LOG(ERROR) << "Unable to parse preemption notice's death time: " << err; CancelPreemptionBarrier(); return; } // Trigger protocol in a separate thread: compute max call counter. sync_protocol_thread_ = absl::WrapUnique(env_->StartThread( {}, "PreemptionSyncManager_SyncProtocol", std::bind(&PreemptionSyncManagerImpl::ComputeSyncCallCounter, this, death_time))); }); return absl::OkStatus(); } [agent = agent_, task_name](absl::StatusOr<absl::Time> death_time) { if (!death_time.ok()) { // The preemption notifier invokes callback with Cancelled error when // its being destructed. if (errors::IsCancelled(death_time.status())) { LOG(INFO) << "Preemption sync protocol cancelled by notifier: " << death_time.status() << ". This is expected during program shutdown."; } else { LOG(ERROR) << "Error from preemption notifier: " << death_time.status(); } return; } notified_metric->GetCell()->Set(true); // Notify coordination service about preemption notice. const absl::Status s = agent->InsertKeyValue( kPreemptionNoticeKey, absl::FormatTime(*death_time)); LOG(INFO) << "Notified coordination service that this task will " "be preempted at " << *death_time << ". absl::Status: " << s; }); [this, agent = agent_](absl::StatusOr<std::string> status_or_death_time) { if (errors::IsCancelled(status_or_death_time.status())) { // The agent cancels pending GetKeyValue RPCs because of shutdown, // so simply log and return. LOG(INFO) << "Cancelled call to retrieve preemption notice. This is " "expected upon program shutdown."; return; } else if (!status_or_death_time.ok()) { LOG(WARNING) << "Failed to retrieve preemption notice from " "coordination service: " << status_or_death_time.status() << ". This is only expected if one of the tasks is unhealthy." " Check the logs for the actual root cause."; // Notify other tasks to not wait at the barrier. Note: // CancelPreemptionBarrier() cannot be used because this may be // triggered after preemption sync manager has been destroyed. agent->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); return; } std::string err; absl::Time death_time; if (absl::ParseTime(absl::RFC3339_full, *status_or_death_time, &death_time, &err)) { LOG(INFO) << "Received preemption notice with death_time " << death_time; } else { LOG(ERROR) << "Unable to parse preemption notice's death time: " << err; CancelPreemptionBarrier(); return; } // Trigger protocol in a separate thread: compute max call counter. sync_protocol_thread_ = absl::WrapUnique(env_->StartThread( {}, "PreemptionSyncManager_SyncProtocol", std::bind(&PreemptionSyncManagerImpl::ComputeSyncCallCounter, this, death_time))); }); kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); void PreemptionSyncManagerImpl::ComputeSyncCallCounter(absl::Time death_time) { // 1. If death time is in the distant future, sleep until there's // `kProtocolDuration` left until death time before we begin the protocol. const absl::Duration remaining_time = death_time - absl::Now(); if (remaining_time > kProtocolDuration) { LOG(INFO) << "Will begin preemption sync protocol in " << remaining_time; const absl::Duration sleep_time = remaining_time - kProtocolDuration; if (shutdown_.WaitForNotificationWithTimeout(sleep_time)) { // If shutdown is triggered midway, exit thread immediately. LOG(WARNING) << "Shutdown is triggered before preemption sync protocol has begun."; CancelPreemptionBarrier(); return; } } // 2. Send coordination service the task's current call counter. Hold the lock // to prevent updates to `call_counter_` until the protocol completes and this // function exits, implying that we have decided on a new // `preemption_sync_counter_` or the protocol failed. This ensures correctness // of the preemption sync protocol. mutex_lock l(mu_); const absl::Status notified_status = agent_->InsertKeyValue( current_call_counter_key_, std::to_string(call_counter_)); if (!notified_status.ok()) { LOG(ERROR) << "Preemption sync failed - could not inform service of " "current call counter: " << notified_status; CancelPreemptionBarrier(); return; } // 3. Impose a barrier to wait until everybody sends their current call // counter. const absl::Status barrier_status = agent_->WaitAtBarrier(kPreemptionBarrier, kPreemptionBarrierTimeout, {}); if (!barrier_status.ok()) { LOG(ERROR) << "Preemption sync barrier failed: " << barrier_status; return; } // 4. Retrieve every task's current call counter. absl::StatusOr<std::vector<KeyValueEntry>> all_counters = agent_->GetKeyValueDir(kPreemptionCounterDirKey); if (!all_counters.ok()) { LOG(ERROR) << "Preemption sync failed - unable to retrieve call counters: " << all_counters.status(); return; } // 5. Compute the fastest task's call counter. // Note: Each task should retrieve the same set of call counters and arrive at // the same maximum. We have to calculate this max within each task because // coordination service does not provide GetMaxKeyValue(). int64_t max_counter = kPreemptionSyncUnsetCounter; for (const auto& kv : *all_counters) { int64_t call_counter; if (!absl::SimpleAtoi(kv.value(), &call_counter)) { LOG(ERROR) << "Preemption sync failed - failed to parse preemption call " "counter: " << kv.DebugString(); return; } max_counter = std::max(max_counter, call_counter); } if (max_counter == kPreemptionSyncUnsetCounter) { LOG(ERROR) << "Preemption sync failed - no call counters found."; return; } // 6. Set sync point to be the next possible call counter of the fastest task. preemption_sync_counter_ = max_counter + 1; LOG(INFO) << "Preemption sync counter is set: " << preemption_sync_counter_; set_sync_point_metric->GetCell()->Set(true); } void PreemptionSyncManagerImpl::CancelPreemptionBarrier() { agent_->CancelBarrierAsync( kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); } kPreemptionBarrier, [](const absl::Status& status) { if (!status.ok()) { LOG(ERROR) << "Failed to cancel preemption barrier: " << status; } }); bool PreemptionSyncManagerImpl::ReachedSyncPoint(int step_counter) { // Record that this API was called at least once. sync_usage_metric->GetCell()->Set(true); // Note: if a preemption notice has been received and ComputeSyncCallCounter() // is ongoing , this method will be blocked until it acquires the lock. This // prevents updates to `call_counter_` while `preemption_sync_counter_` is // being computed, which ensures correctness of the preemption sync protocol. mutex_lock l(mu_); // Track current call. call_counter_ = step_counter; VLOG(3) << "Current call counter: " << call_counter_ << ", Preemption sync point: " << preemption_sync_counter_; const bool reached_sync_point = preemption_sync_counter_ == call_counter_; if (reached_sync_point) { // Record that this job reached the sync point. reached_sync_point_metric->GetCell()->Set(true); } return reached_sync_point; } VLOG(3) << "Current call counter: " << call_counter_ std::unique_ptr<PreemptionSyncManager> CreatePreemptionSyncManager() { return std::make_unique<PreemptionSyncManagerImpl>(); }
#include "xla/tsl/distributed_runtime/preemption/preemption_sync_manager.h" #include <memory> #include <string> #include <utility> #include "grpcpp/server.h" #include "grpcpp/server_builder.h" #include "grpcpp/support/channel_arguments.h" #include "absl/memory/memory.h" #include "absl/time/clock.h" #include "absl/time/time.h" #include "xla/tsl/distributed_runtime/coordination/coordination_client.h" #include "xla/tsl/distributed_runtime/coordination/coordination_service.h" #include "xla/tsl/distributed_runtime/coordination/coordination_service_agent.h" #include "xla/tsl/distributed_runtime/preemption/preemption_notifier.h" #include "xla/tsl/distributed_runtime/rpc/async_service_interface.h" #include "xla/tsl/distributed_runtime/rpc/coordination/grpc_coordination_client.h" #include "xla/tsl/distributed_runtime/rpc/coordination/grpc_coordination_service_impl.h" #include "tsl/platform/env.h" #include "tsl/platform/errors.h" #include "tsl/platform/status.h" #include "tsl/platform/test.h" #include "tsl/platform/threadpool.h" #include "tsl/protobuf/coordination_config.pb.h" class PreemptionSyncManagerTest : public ::testing::Test { protected: PreemptionSyncManagerTest() { // Setup coordination service. StartCoordinationService(); InitializeAndConnectCoordinationAgents(); // Create preempt sync manager for task 1. auto preempt_notifier = std::make_unique<FakePreemptionNotifier>(); preempt_notifier_ = preempt_notifier.get(); TF_CHECK_OK(preempt_sync_mgr_->Initialize(coord_agent_.get(), std::move(preempt_notifier))); // Create preempt sync manager for task 2. auto preempt_notifier2 = std::make_unique<FakePreemptionNotifier>(); preempt_notifier2_ = preempt_notifier2.get(); TF_CHECK_OK(preempt_sync_mgr2_->Initialize(coord_agent2_.get(), std::move(preempt_notifier2))); } ~PreemptionSyncManagerTest() override { // Tear down coordination service objects in order. preempt_sync_mgr_ = nullptr; preempt_sync_mgr2_ = nullptr; coord_agent_ = nullptr; coord_agent2_ = nullptr; coord_service_ = nullptr; static_cast<tsl::GrpcCoordinationServiceImpl*>(coord_rpc_service_.get()) ->SetCoordinationServiceInstance(nullptr); grpc_server_->Shutdown(); coord_rpc_service_->Shutdown(); } // `to_task1` toggles which of the two tasks receives preemption notice. void SendPreemptionNotice(absl::Time death_time = absl::Now(), bool to_task1 = true) { if (to_task1) { preempt_notifier_->AnnounceDeath(death_time); } else { preempt_notifier2_->AnnounceDeath(death_time); } // Block main thread for a short while to allow preemption sync manager to // process the notice. Env::Default()->SleepForMicroseconds( absl::ToInt64Microseconds(absl::Seconds(1))); } // Report to coordiation service that task two is unhealthy. void SimulateUnhealthyTaskTwo() { CoordinatedTask task2; task2.set_job_name(kJobName); task2.set_task_id(1); TF_CHECK_OK( coord_service_->ReportTaskError(task2, errors::Internal("test_error"))); } // Allow access to objects under test. std::unique_ptr<PreemptionSyncManager> preempt_sync_mgr_ = CreatePreemptionSyncManager(); std::unique_ptr<PreemptionSyncManager> preempt_sync_mgr2_ = CreatePreemptionSyncManager(); protected: // Utility methods to set up coordination service and agents. void StartCoordinationService() { ::grpc::ServerBuilder builder; coord_service_ = EnableCoordinationService(); coord_compute_pool_ = std::make_unique<thread::ThreadPool>( Env::Default(), "CoordinationServiceRpcHandler", /*num_threads=*/1); coord_rpc_service_ = std::make_unique<GrpcCoordinationServiceImpl>( coord_compute_pool_.get(), &builder); auto* grpc_coord_service = static_cast<GrpcCoordinationServiceImpl*>(coord_rpc_service_.get()); grpc_coord_service->SetCoordinationServiceInstance(coord_service_.get()); grpc_server_ = builder.BuildAndStart(); coord_rpc_thread_ = absl::WrapUnique(Env::Default()->StartThread( /*thread_options=*/{}, /*name=*/"CoordinationServiceHandleRPCsLoop", [service = coord_rpc_service_.get()]() { service->HandleRPCsLoop(); })); } std::unique_ptr<CoordinationServiceInterface> EnableCoordinationService() { CoordinationServiceConfig config; config.set_service_type("standalone"); CoordinatedJob* job = config.mutable_coordinated_job_list()->Add(); job->set_name(kJobName); job->set_num_tasks(2); return CoordinationServiceInterface::EnableCoordinationService( Env::Default(), config, /*cache=*/nullptr); } void InitializeAndConnectCoordinationAgents() { std::unique_ptr<CoordinationClient> coord_client = absl::WrapUnique(NewGrpcCoordinationClient( grpc_server_->InProcessChannel(::grpc::ChannelArguments()))); std::unique_ptr<CoordinationClient> coord_client2 = absl::WrapUnique(NewGrpcCoordinationClient( grpc_server_->InProcessChannel(::grpc::ChannelArguments()))); auto error_fn = [](const absl::Status& status) { LOG(ERROR) << "Coordination service agent in error status: " << status; }; TEST_F(PreemptionSyncManagerTest, ShutdownTasksWithoutPreemption) { int step_counter = 0; // Simulate task doing work and making progress. EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter++)); EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter++)); // Shutdown coordination service agents. TF_CHECK_OK(coord_agent_->Shutdown()); TF_CHECK_OK(coord_agent2_->Shutdown()); // Protocol is not triggerred, so there should be no sync point. EXPECT_FALSE(preempt_sync_mgr_->ReachedSyncPoint(step_counter++)); }
PreemptNotifierTest_DestructorCancelsPendingCalls
xla/tsl/distributed_runtime/preemption/preemption_notifier_test.cc
~SigtermNotifier() override { // Trigger shutdown logic in listener thread. shutdown_notification_.Notify(); } SigtermNotifier::SigtermNotifier(Env* env) : PreemptionNotifier(env) { sigterm_received.store(false); StartListenerThread(); #if defined(PLATFORM_GOOGLE) thread::signal::Token unused_token; thread::signal::AddHandler( SIGTERM, thread::Executor::DefaultExecutor(), []() { sigterm_received.store(true); }, /*flags=*/0, // Don't override existing signal handlers. &unused_token); #else std::signal(SIGTERM, [](int signal) { sigterm_received.store(true); }); #endif } void SigtermNotifier::StartListenerThread() { preempt_listener_thread_.reset( GetEnv()->StartThread({}, "PreemptionNotifier_Listen", [this]() { // Poll for SIGTERM receipt every kListenInterval. while (!sigterm_received.load()) { if (shutdown_notification_.WaitForNotificationWithTimeout( kListenInterval)) { // Shutdown: // 1) Cancel any pending callbacks and blocking WillBePreemptedAt() // calls. NotifyRegisteredListeners( errors::Cancelled("Preemption notifier is being deleted.")); // 2) Exit listener thread. return; } } const absl::Time death_time = absl::Now(); LOG(WARNING) << "SIGTERM caught at " << death_time; // Notify registered listeners. NotifyRegisteredListeners(death_time); })); } GetEnv()->StartThread({}, "PreemptionNotifier_Listen", [this]() { // Poll for SIGTERM receipt every kListenInterval. while (!sigterm_received.load()) { if (shutdown_notification_.WaitForNotificationWithTimeout( kListenInterval)) { // Shutdown: // 1) Cancel any pending callbacks and blocking WillBePreemptedAt() // calls. NotifyRegisteredListeners( errors::Cancelled("Preemption notifier is being deleted.")); // 2) Exit listener thread. return; } } const absl::Time death_time = absl::Now(); LOG(WARNING) << "SIGTERM caught at " << death_time; // Notify registered listeners. NotifyRegisteredListeners(death_time); })); absl::StatusOr<absl::Time> PreemptionNotifier::WillBePreemptedAt() { absl::Notification n; absl::StatusOr<absl::Time> result; WillBePreemptedAtAsync( [&n, &result](absl::StatusOr<absl::Time> async_result) { result = async_result; n.Notify(); }); n.WaitForNotification(); return result; } [&n, &result](absl::StatusOr<absl::Time> async_result) { result = async_result; n.Notify(); }); void PreemptionNotifier::WillBePreemptedAtAsync(PreemptTimeCallback callback) { mutex_lock l(mu_); if (death_time_ == kUnsetDeathTime) { // Did not receive preemption notice yet. callbacks_.push_back(std::move(callback)); } else { // Already received preemption notice, respond immediately. callback(death_time_); } } void PreemptionNotifier::NotifyRegisteredListeners( absl::StatusOr<absl::Time> death_time) { mutex_lock l(mu_); if (death_time.ok()) { death_time_ = death_time.value(); } for (const auto& callback : callbacks_) { callback(death_time); } callbacks_.clear(); } REGISTER_PREEMPTION_NOTIFIER(
#include "xla/tsl/distributed_runtime/preemption/preemption_notifier.h" #include <csignal> #include <functional> #include <memory> #include <utility> #include "absl/synchronization/notification.h" #include "absl/time/clock.h" #include "absl/time/time.h" #include "tsl/platform/env.h" #include "tsl/platform/errors.h" #include "tsl/platform/status.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test.h" #include "thread/executor.h" #include "thread/signal.h" class PreemptNotifierTest : public ::testing::Test { public: PreemptNotifierTest() { #if defined(PLATFORM_GOOGLE) // Override default test SIGTERM handler so that test does not exit // prematurely. thread::signal::Token unused_token; thread::signal::AddHandler( SIGTERM, thread::Executor::DefaultExecutor(), []() {}, thread::signal::kOverrideDefault, &unused_token); #endif } }; TEST_F(PreemptNotifierTest, DestructorCancelsPendingCalls) { auto env = Env::Default(); std::unique_ptr<PreemptionNotifier> preempt_notifier = PreemptionNotifier::CreatePreemptionNotifier("sigterm", env); absl::StatusOr<absl::Time> result; absl::Notification n; preempt_notifier->WillBePreemptedAtAsync( [&result, &n](absl::StatusOr<absl::Time> status_or_time) { result = status_or_time; n.Notify(); }); // Invoke dtor. preempt_notifier = nullptr; n.WaitForNotification(); // Verify that pending callbacks are cancelled. EXPECT_TRUE(errors::IsCancelled(result.status())); }
PreemptNotifierTest_WillBePreemptedAt
xla/tsl/distributed_runtime/preemption/preemption_notifier_test.cc
~SigtermNotifier() override { // Trigger shutdown logic in listener thread. shutdown_notification_.Notify(); } SigtermNotifier::SigtermNotifier(Env* env) : PreemptionNotifier(env) { sigterm_received.store(false); StartListenerThread(); #if defined(PLATFORM_GOOGLE) thread::signal::Token unused_token; thread::signal::AddHandler( SIGTERM, thread::Executor::DefaultExecutor(), []() { sigterm_received.store(true); }, /*flags=*/0, // Don't override existing signal handlers. &unused_token); #else std::signal(SIGTERM, [](int signal) { sigterm_received.store(true); }); #endif } void SigtermNotifier::StartListenerThread() { preempt_listener_thread_.reset( GetEnv()->StartThread({}, "PreemptionNotifier_Listen", [this]() { // Poll for SIGTERM receipt every kListenInterval. while (!sigterm_received.load()) { if (shutdown_notification_.WaitForNotificationWithTimeout( kListenInterval)) { // Shutdown: // 1) Cancel any pending callbacks and blocking WillBePreemptedAt() // calls. NotifyRegisteredListeners( errors::Cancelled("Preemption notifier is being deleted.")); // 2) Exit listener thread. return; } } const absl::Time death_time = absl::Now(); LOG(WARNING) << "SIGTERM caught at " << death_time; // Notify registered listeners. NotifyRegisteredListeners(death_time); })); } GetEnv()->StartThread({}, "PreemptionNotifier_Listen", [this]() { // Poll for SIGTERM receipt every kListenInterval. while (!sigterm_received.load()) { if (shutdown_notification_.WaitForNotificationWithTimeout( kListenInterval)) { // Shutdown: // 1) Cancel any pending callbacks and blocking WillBePreemptedAt() // calls. NotifyRegisteredListeners( errors::Cancelled("Preemption notifier is being deleted.")); // 2) Exit listener thread. return; } } const absl::Time death_time = absl::Now(); LOG(WARNING) << "SIGTERM caught at " << death_time; // Notify registered listeners. NotifyRegisteredListeners(death_time); })); absl::StatusOr<absl::Time> PreemptionNotifier::WillBePreemptedAt() { absl::Notification n; absl::StatusOr<absl::Time> result; WillBePreemptedAtAsync( [&n, &result](absl::StatusOr<absl::Time> async_result) { result = async_result; n.Notify(); }); n.WaitForNotification(); return result; } [&n, &result](absl::StatusOr<absl::Time> async_result) { result = async_result; n.Notify(); }); void PreemptionNotifier::WillBePreemptedAtAsync(PreemptTimeCallback callback) { mutex_lock l(mu_); if (death_time_ == kUnsetDeathTime) { // Did not receive preemption notice yet. callbacks_.push_back(std::move(callback)); } else { // Already received preemption notice, respond immediately. callback(death_time_); } } void PreemptionNotifier::NotifyRegisteredListeners( absl::StatusOr<absl::Time> death_time) { mutex_lock l(mu_); if (death_time.ok()) { death_time_ = death_time.value(); } for (const auto& callback : callbacks_) { callback(death_time); } callbacks_.clear(); } REGISTER_PREEMPTION_NOTIFIER(
#include "xla/tsl/distributed_runtime/preemption/preemption_notifier.h" #include <csignal> #include <functional> #include <memory> #include <utility> #include "absl/synchronization/notification.h" #include "absl/time/clock.h" #include "absl/time/time.h" #include "tsl/platform/env.h" #include "tsl/platform/errors.h" #include "tsl/platform/status.h" #include "tsl/platform/statusor.h" #include "tsl/platform/test.h" #include "thread/executor.h" #include "thread/signal.h" class PreemptNotifierTest : public ::testing::Test { public: PreemptNotifierTest() { #if defined(PLATFORM_GOOGLE) // Override default test SIGTERM handler so that test does not exit // prematurely. thread::signal::Token unused_token; thread::signal::AddHandler( SIGTERM, thread::Executor::DefaultExecutor(), []() {}, thread::signal::kOverrideDefault, &unused_token); #endif } }; TEST_F(PreemptNotifierTest, WillBePreemptedAt) { auto env = Env::Default(); std::unique_ptr<PreemptionNotifier> preempt_notifier = PreemptionNotifier::CreatePreemptionNotifier("sigterm", env); absl::Time start_time = absl::Now(); env->SchedClosureAfter(/*micros=*/absl::ToInt64Microseconds(absl::Seconds(1)), []() { std::raise(SIGTERM); }); // Preempt time should be current timestamp. absl::StatusOr<absl::Time> result = preempt_notifier->WillBePreemptedAt(); TF_CHECK_OK(result.status()); absl::Time preempt_time = result.value(); // Make sure that preempt time is approximately correct. absl::Duration time_diff = preempt_time - start_time; // Signal was raised 1 second after start time. EXPECT_GT(time_diff, absl::Seconds(1.0)); // Listen to signal once per second, so we should catch within 2 seconds. EXPECT_LT(time_diff, absl::Seconds(3)); }
ReferenceUtilTest_ApplyElementwise2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, ApplyElementwise2D) { Array2D<float> a({{1, 2}, {3, 4}}); Array2D<float> b({{10, 20}, {30, 40}}); Array2D<float> c({{100, 200}, {300, 400}}); auto actual = ReferenceUtil::ApplyElementwise2D( [](float x, float y, float z) { return 100 * x + 10 * y + z; }, a, b, c); auto actual_literal = LiteralUtil::CreateR2FromArray2D(*actual); LiteralTestUtil::ExpectR2Near({{300.f, 600.f}, {900.f, 1200.f}}, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_ConvArray3DWithSamePadding
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; };
ReferenceUtilTest_ConvArray3DWithValidPadding
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; };
ReferenceUtilTest_ConvGeneralDimensionsWithSamePadding
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; };
ReferenceUtilTest_ConvGeneralDimensionsWithValidPadding
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; };
ReferenceUtilTest_ConvWithSamePadding
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, ConvWithSamePadding) { Array4D<float> input(1, 1, 4, 4); // clang-format off input.FillWithYX(Array2D<float>({ {1, 2, 3, 4 }, {5, 6, 7, 8 }, {9, 10, 11, 12}, {13, 14, 15, 16}, })); // clang-format on Array4D<float> weights(1, 1, 2, 2); // clang-format off weights.FillWithYX(Array2D<float>({ {5, 6}, {7, 8}, })); // clang-format on std::unique_ptr<Array4D<float>> actual = ReferenceUtil::ConvArray4D(input, weights, {1, 1}, Padding::kSame); Array4D<float> expected(1, 1, 4, 4); // clang-format off expected.FillWithYX(Array2D<float>({ {100, 126, 152, 76}, {204, 230, 256, 124}, {308, 334, 360, 172}, {149, 160, 171, 80}, })); // clang-format on auto actual_literal = LiteralUtil::CreateR4FromArray4D(*actual); LiteralTestUtil::ExpectR4NearArray4D<float>(expected, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_ConvWithValidPadding
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, ConvWithValidPadding) { Array4D<float> input(1, 1, 4, 4); // clang-format off input.FillWithYX(Array2D<float>({ {1, 2, 3, 4 }, {5, 6, 7, 8 }, {9, 10, 11, 12}, {13, 14, 15, 16}, })); // clang-format on Array4D<float> weights(1, 1, 2, 2); // clang-format off weights.FillWithYX(Array2D<float>({ {5, 6}, {7, 8}, })); // clang-format on std::unique_ptr<Array4D<float>> actual = ReferenceUtil::ConvArray4D(input, weights, {1, 1}, Padding::kValid); Array4D<float> expected(1, 1, 3, 3); // clang-format off expected.FillWithYX(Array2D<float>({ {1*5+2*6+5*7+6*8, 126, 152}, {204, 230, 256}, {308, 334, 11*5+12*6+15*7+16*8}, })); // clang-format on auto actual_literal = LiteralUtil::CreateR4FromArray4D(*actual); LiteralTestUtil::ExpectR4NearArray4D<float>(expected, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_MapArray3D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, MapArray3D) { auto identity = [](float value) { return std::log(std::exp(value)); }; Array3D<float> input(2, 3, 4); input.FillIota(0); auto result = ReferenceUtil::MapArray3D(input, identity); auto actual_literal = LiteralUtil::CreateR3FromArray3D(*result); LiteralTestUtil::ExpectR3NearArray3D(input, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_MapArray4D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, MapArray4D) { auto input = std::make_unique<Array4D<float>>(/*planes=*/2, /*depth=*/3, /*height=*/4, /*width=*/5); input->FillWithMultiples(1.0f); auto multiply_by_two = [](float value) { return 2 * value; }; auto result = ReferenceUtil::MapArray4D(*input, multiply_by_two); auto actual_literal = LiteralUtil::CreateR4FromArray4D(*result); Array4D<float> expected(/*planes=*/2, /*depth=*/3, /*height=*/4, /*width=*/5); expected.FillWithMultiples(2.0f); LiteralTestUtil::ExpectR4NearArray4D(expected, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_MapWithIndexArray2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, MapWithIndexArray2D) { auto add_index = [](float value, int64_t row, int64_t col) { return value + row + col; }; auto result = ReferenceUtil::MapWithIndexArray2D(*matrix_, add_index); auto actual_literal = LiteralUtil::CreateR2FromArray2D(*result); LiteralTestUtil::ExpectR2Near<float>({{1.f, 3.f, 5.f}, {5.f, 7.f, 9.f}}, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_MapWithIndexArray4D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, MapWithIndexArray4D) { auto input = std::make_unique<Array4D<float>>(/*planes=*/2, /*depth=*/3, /*height=*/4, /*width=*/5); input->FillWithMultiples(1.0f); auto subtract_index = [](float value, int64_t plane, int64_t depth, int64_t height, int64_t width) { return value - (3 * 4 * 5 * plane + 4 * 5 * depth + 5 * height + width); }; auto result = ReferenceUtil::MapWithIndexArray4D(*input, subtract_index); auto actual_literal = LiteralUtil::CreateR4FromArray4D(*result); Array4D<float> expected(/*planes=*/2, /*depth=*/3, /*height=*/4, /*width=*/5); expected.Fill(0.0f); LiteralTestUtil::ExpectR4NearArray4D(expected, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_MatmulArray2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, MatmulArray2D) { Array2D<float> rhs({ {7.f, 8.f}, {9.f, 10.f}, {11.f, 12.f}, }); auto result = ReferenceUtil::MatmulArray2D(*matrix_, rhs); auto actual_literal = LiteralUtil::CreateR2FromArray2D(*result); LiteralTestUtil::ExpectR2Near<float>({{58.f, 64.f}, {139.f, 154.f}}, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_Reduce4Dto1DZeroSizedArray
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, Reduce4Dto1DZeroSizedArray) { auto result = LiteralUtil::CreateR1<float>(ReferenceUtil::Reduce4DTo1D( Array4D<float>(1, 0, 1, 1), /*init=*/0, /*dims=*/{0, 1, 2}, [](float a, float b) { return a + b; })); LiteralTestUtil::ExpectR1Equal<float>({0}, result); }
ReferenceUtilTest_ReduceToColArray2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, ReduceToColArray2D) { auto add = [](float lhs, float rhs) { return lhs + rhs; }; auto result = ReferenceUtil::ReduceToColArray2D(*matrix_, 0.0f, add); auto actual_literal = LiteralUtil::CreateR1<float>(*result); LiteralTestUtil::ExpectR1Near<float>({6.f, 15.f}, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_ReduceToRowArray2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, ReduceToRowArray2D) { auto add = [](float lhs, float rhs) { return lhs + rhs; }; auto result = ReferenceUtil::ReduceToRowArray2D(*matrix_, 0.0f, add); auto actual_literal = LiteralUtil::CreateR1<float>(*result); LiteralTestUtil::ExpectR1Near<float>({5.f, 7.f, 9.f}, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_SliceArray2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, SliceArray2D) { auto result = ReferenceUtil::Slice2D(*matrix_, {{0, 0}}, {{2, 2}}, {{1, 1}}); auto actual_literal = LiteralUtil::CreateR2FromArray2D(*result); LiteralTestUtil::ExpectR2Near<float>({{1.f, 2.f}, {4.f, 5.f}}, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_SliceArray3D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; };
ReferenceUtilTest_SliceArray4D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; };
ReferenceUtilTest_SliceStridedArray2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, SliceStridedArray2D) { auto result = ReferenceUtil::Slice2D(*matrix_, {{0, 0}}, {{2, 3}}, {{1, 2}}); auto actual_literal = LiteralUtil::CreateR2FromArray2D(*result); LiteralTestUtil::ExpectR2Near<float>({{1.f, 3.f}, {4.f, 6.f}}, actual_literal, ErrorSpec(0.0001)); }
ReferenceUtilTest_SliceStridedArray3D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; };
ReferenceUtilTest_SliceStridedArray4D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; };
ReferenceUtilTest_TransposeArray2D
xla/reference_util_test.cc
/* static */ std::unique_ptr<Array2D<double>> ReferenceUtil::Array2DF32ToF64( const Array2D<float>& input) { auto result = std::make_unique<Array2D<double>>(input.height(), input.width()); for (int64_t rowno = 0; rowno < input.height(); ++rowno) { for (int64_t colno = 0; colno < input.width(); ++colno) { (*result)(rowno, colno) = input(rowno, colno); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ConvArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding) { return ConvArray3DGeneralDimensionsDilated( lhs, rhs, kernel_stride, padding, 1, 1, XlaBuilder::CreateDefaultConvDimensionNumbers(1)); } ReferenceUtil::ConvArray3DGeneralDimensionsDilated( const Array3D<float>& lhs, const Array3D<float>& rhs, int64_t kernel_stride, Padding padding, int64_t lhs_dilation, int64_t rhs_dilation, const ConvolutionDimensionNumbers& dnums) { CHECK_EQ(dnums.input_spatial_dimensions_size(), 1); CHECK_EQ(dnums.kernel_spatial_dimensions_size(), 1); CHECK_EQ(dnums.output_spatial_dimensions_size(), 1); // Reuse the code for Array4D-convolution by extending the 3D input into a 4D // array by adding a fourth dummy dimension of size 1 without stride, padding // and dilation. Array4D<float> a4dlhs(lhs.n1(), lhs.n2(), lhs.n3(), 1); a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); Array4D<float> a4drhs(rhs.n1(), rhs.n2(), rhs.n3(), 1); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); // Add a second dummy spatial dimensions. ConvolutionDimensionNumbers dnums2d = dnums; dnums2d.add_input_spatial_dimensions(3); dnums2d.add_kernel_spatial_dimensions(3); dnums2d.add_output_spatial_dimensions(3); std::unique_ptr<Array4D<float>> convr4 = ConvArray4DGeneralDimensionsDilated( a4dlhs, a4drhs, {kernel_stride, 1}, padding, {lhs_dilation, 1}, {rhs_dilation, 1}, dnums2d); auto convr3 = std::make_unique<Array3D<float>>( convr4->planes(), convr4->depth(), convr4->height()); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); return convr3; } a4dlhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = lhs.operator()(indices[0], indices[1], indices[2]); }); a4drhs.Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); *value_ptr = rhs.operator()(indices[0], indices[1], indices[2]); }); convr4->Each([&](absl::Span<const int64_t> indices, float* value_ptr) { CHECK_EQ(indices[3], 0); convr3->operator()(indices[0], indices[1], indices[2]) = *value_ptr; }); /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ConvArray4D( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { return ConvArray4DGeneralDimensions( lhs, rhs, kernel_stride, padding, XlaBuilder::CreateDefaultConvDimensionNumbers()); } ReferenceUtil::SeparableConvArray4D(const Array4D<float>& input, const Array4D<float>& depthwise_weights, const Array4D<float>& pointwise_weights, std::pair<int64_t, int64_t> kernel_stride, Padding padding) { const int64_t depth_multiplier = depthwise_weights.planes(); CHECK_EQ(pointwise_weights.depth(), input.depth() * depth_multiplier); // Combine the two weights by reducing the depth_multiplier, so that we can // apply a single convolution on the combined weights. Array4D<float> weights(pointwise_weights.planes(), input.depth(), depthwise_weights.height(), depthwise_weights.width()); for (int64_t kx = 0; kx < depthwise_weights.width(); ++kx) { for (int64_t ky = 0; ky < depthwise_weights.height(); ++ky) { for (int64_t kz = 0; kz < input.depth(); ++kz) { for (int64_t out = 0; out < pointwise_weights.planes(); ++out) { float weight = 0.0; for (int64_t depth = 0; depth < depth_multiplier; ++depth) { weight += depthwise_weights(depth, kz, ky, kx) * pointwise_weights(out, depth + kz * depth_multiplier, 0, 0); } weights(out, kz, ky, kx) = weight; } } } } return ConvArray4D(input, weights, kernel_stride, padding); } /* static */ int64_t ReferenceUtil::WindowCount(int64_t unpadded_width, int64_t window_len, int64_t stride, Padding padding) { if (padding == Padding::kValid) { return window_util::StridedBound(unpadded_width, window_len, stride); } return tsl::MathUtil::CeilOfRatio(unpadded_width, stride); } ReferenceUtil::ReduceWindow1DGeneric( absl::Span<const float> operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { CHECK_EQ(window.size(), 1); CHECK_EQ(stride.size(), 1); CHECK_EQ(padding.size(), 1); int64_t padded_width = padding[0].first + operand.size() + padding[0].second; int64_t stride_amount = stride[0]; int64_t window_size = window[0]; int64_t result_size = window_util::StridedBound(padded_width, window_size, stride_amount); int64_t pad_low = padding[0].first; auto result = std::make_unique<std::vector<float>>(result_size); // Do a full 1D reduce window. for (int64_t i0 = 0; i0 < result_size; ++i0) { int64_t i0_base = i0 * stride_amount - pad_low; float val = init; for (int64_t i0_win = 0; i0_win < window_size; ++i0_win) { if (i0_base + i0_win >= 0 && i0_base + i0_win < operand.size()) { val = reduce_func(val, operand[i0_base + i0_win]); } } (*result)[i0] = val; } return result; } ReferenceUtil::ReduceWindow1DAdd(absl::Span<const float> operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; std::vector<int64_t> dim_lengths{static_cast<int64_t>(operand.size())}; return ReduceWindow1DGeneric( operand, init, add_reduce, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::ReduceWindow3DAdd( const Array3D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } auto result = std::make_unique<Array3D<float>>( window_counts[0], window_counts[1], window_counts[2]); for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3()) { val += operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win); } } } } (*result)(i0, i1, i2) = val; } } } return result; } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; return ReduceWindow4DGeneric( operand, init, reduce_func, window, stride, xla::MakePadding(dim_lengths, window, stride, padding)); } ReferenceUtil::ReduceWindow4DGeneric( const Array4D<float>& operand, float init, absl::FunctionRef<float(float, float)> reduce_func, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, absl::Span<const std::pair<int64_t, int64_t>> padding) { std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { int64_t padded_width = padding[i].first + dim_lengths[i] + padding[i].second; window_counts[i] = window_util::StridedBound(padded_width, window[i], stride[i]); pad_low[i] = padding[i].first; } auto result = std::make_unique<Array4D<float>>( window_counts[0], window_counts[1], window_counts[2], window_counts[3]); // Do a full 4D reduce window. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; float val = init; for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { val = reduce_func( val, operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win)); } } } } } (*result)(i0, i1, i2, i3) = val; } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::ReduceWindow4DAdd( const Array4D<float>& operand, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, Padding padding) { const auto add_reduce = [](float arg1, float arg2) { return arg1 + arg2; }; return ReduceWindow4DGeneric(operand, init, add_reduce, window, stride, padding); } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::BatchNorm4D( const Array4D<float>& input, const Array4D<float>& mean, const Array4D<float>& var, const Array4D<float>& scale, const Array4D<float>& offset, float epsilon) { auto normalized = *MapArray4D(input, mean, [](float a, float b) { return a - b; }); normalized = *MapArray4D(normalized, var, [&](float a, float b) { return a / std::sqrt(b + epsilon); }); normalized = *MapArray4D(normalized, scale, [](float a, float b) { return a * b; }); return MapArray4D(normalized, offset, [](float a, float b) { return a + b; }); } ReferenceUtil::SelectAndScatter4DGePlus(const Array4D<float>& operand, const Array4D<float>& source, float init, absl::Span<const int64_t> window, absl::Span<const int64_t> stride, bool same_padding) { Padding padding = same_padding ? Padding::kSame : Padding::kValid; auto result = std::make_unique<Array4D<float>>(operand.n1(), operand.n2(), operand.n3(), operand.n4()); std::vector<int64_t> dim_lengths{operand.n1(), operand.n2(), operand.n3(), operand.n4()}; auto padding_both = xla::MakePadding(dim_lengths, window, stride, padding); // Fill the output, with the initial value. result->Fill(init); std::vector<int64_t> window_counts(window.size(), 0); std::vector<int64_t> pad_low(window.size(), 0); for (int64_t i = 0; i < window.size(); ++i) { window_counts[i] = WindowCount(dim_lengths[i], window[i], stride[i], padding); pad_low[i] = padding_both[i].first; } CHECK_EQ(window_counts[0], source.n1()); CHECK_EQ(window_counts[1], source.n2()); CHECK_EQ(window_counts[2], source.n3()); CHECK_EQ(window_counts[3], source.n4()); // Do a full 4D select and Scatter. for (int64_t i0 = 0; i0 < window_counts[0]; ++i0) { for (int64_t i1 = 0; i1 < window_counts[1]; ++i1) { for (int64_t i2 = 0; i2 < window_counts[2]; ++i2) { for (int64_t i3 = 0; i3 < window_counts[3]; ++i3) { // Now we are inside a window and need to find the max and the argmax. int64_t i0_base = i0 * stride[0] - pad_low[0]; int64_t i1_base = i1 * stride[1] - pad_low[1]; int64_t i2_base = i2 * stride[2] - pad_low[2]; int64_t i3_base = i3 * stride[3] - pad_low[3]; int64_t scatter_0 = (i0_base >= 0) ? i0_base : 0; int64_t scatter_1 = (i1_base >= 0) ? i1_base : 0; int64_t scatter_2 = (i2_base >= 0) ? i2_base : 0; int64_t scatter_3 = (i3_base >= 0) ? i3_base : 0; float val = operand(scatter_0, scatter_1, scatter_2, scatter_3); for (int64_t i0_win = 0; i0_win < window[0]; ++i0_win) { for (int64_t i1_win = 0; i1_win < window[1]; ++i1_win) { for (int64_t i2_win = 0; i2_win < window[2]; ++i2_win) { for (int64_t i3_win = 0; i3_win < window[3]; ++i3_win) { if (i0_base + i0_win >= 0 && i1_base + i1_win >= 0 && i2_base + i2_win >= 0 && i3_base + i3_win >= 0 && i0_base + i0_win < operand.n1() && i1_base + i1_win < operand.n2() && i2_base + i2_win < operand.n3() && i3_base + i3_win < operand.n4()) { float tmp = operand(i0_base + i0_win, i1_base + i1_win, i2_base + i2_win, i3_base + i3_win); if (tmp > val) { val = tmp; scatter_0 = i0_base + i0_win; scatter_1 = i1_base + i1_win; scatter_2 = i2_base + i2_win; scatter_3 = i3_base + i3_win; } } } } } } (*result)(scatter_0, scatter_1, scatter_2, scatter_3) += source(i0, i1, i2, i3); } } } } return result; } ReferenceUtil::ConvArray4DGeneralDimensions( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, ConvolutionDimensionNumbers dimension_numbers) { return ConvArray4DGeneralDimensionsDilated(lhs, rhs, kernel_stride, padding, {1, 1}, {1, 1}, std::move(dimension_numbers)); } ReferenceUtil::ConvArray4DGeneralDimensionsDilated( const Array4D<float>& lhs, const Array4D<float>& rhs, std::pair<int64_t, int64_t> kernel_stride, Padding padding, std::pair<int64_t, int64_t> lhs_dilation, std::pair<int64_t, int64_t> rhs_dilation, ConvolutionDimensionNumbers dnums) { HloComputation::Builder b("ConvArray4DGeneralDimensionDilated"); auto lhs_literal = LiteralUtil::CreateR4FromArray4D<float>(lhs); auto rhs_literal = LiteralUtil::CreateR4FromArray4D<float>(rhs); std::array<int64_t, 2> ordered_kernel_strides; std::array<int64_t, 2> ordered_input_dimensions; std::array<int64_t, 2> ordered_kernel_dimensions; if (dnums.kernel_spatial_dimensions(0) > dnums.kernel_spatial_dimensions(1)) { ordered_kernel_strides[0] = kernel_stride.second; ordered_kernel_strides[1] = kernel_stride.first; } else { ordered_kernel_strides[0] = kernel_stride.first; ordered_kernel_strides[1] = kernel_stride.second; } ordered_input_dimensions[0] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(0)); ordered_input_dimensions[1] = lhs_literal.shape().dimensions(dnums.input_spatial_dimensions(1)); ordered_kernel_dimensions[0] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0)); ordered_kernel_dimensions[1] = rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1)); std::vector<std::pair<int64_t, int64_t>> paddings = MakePadding(ordered_input_dimensions, ordered_kernel_dimensions, ordered_kernel_strides, padding); CHECK_EQ(paddings.size(), 2); Window window; WindowDimension dim; dim.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(0))); dim.set_stride(kernel_stride.first); dim.set_padding_low(paddings[0].first); dim.set_padding_high(paddings[0].second); dim.set_window_dilation(rhs_dilation.first); dim.set_base_dilation(lhs_dilation.first); *window.add_dimensions() = dim; WindowDimension dim2; dim2.set_size( rhs_literal.shape().dimensions(dnums.kernel_spatial_dimensions(1))); dim2.set_stride(kernel_stride.second); dim2.set_padding_low(paddings[1].first); dim2.set_padding_high(paddings[1].second); dim2.set_window_dilation(rhs_dilation.second); dim2.set_base_dilation(lhs_dilation.second); *window.add_dimensions() = dim2; const Shape shape = ShapeInference::InferConvolveShape( lhs_literal.shape(), rhs_literal.shape(), /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, /*preferred_element_type=*/std::nullopt) .value(); HloInstruction* lhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(lhs_literal))); HloInstruction* rhs_instruction = b.AddInstruction(HloInstruction::CreateConstant(std::move(rhs_literal))); PrecisionConfig precision_config; precision_config.mutable_operand_precision()->Resize( /*new_size=*/2, PrecisionConfig::DEFAULT); b.AddInstruction(HloInstruction::CreateConvolve( shape, lhs_instruction, rhs_instruction, /*feature_group_count=*/1, /*batch_group_count=*/1, window, dnums, precision_config)); HloModuleConfig config; HloModule module("ReferenceUtil", config); auto computation = module.AddEntryComputation(b.Build()); HloEvaluator evaluator; Literal result_literal = evaluator.Evaluate(*computation, {}).value(); CHECK_EQ(result_literal.shape().rank(), 4); auto result = std::make_unique<Array4D<float>>(result_literal.shape().dimensions(0), result_literal.shape().dimensions(1), result_literal.shape().dimensions(2), result_literal.shape().dimensions(3)); result->Each([&](absl::Span<const int64_t> indices, float* value) { *value = result_literal.Get<float>(indices); }); return result; } ReferenceUtil::ReduceToColArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < rows; ++i) { float acc = init; for (int64_t j = 0; j < cols; ++j) { acc = reduce_function(acc, matrix(i, j)); } result->push_back(acc); } return result; } ReferenceUtil::ReduceToRowArray2D( const Array2D<float>& matrix, float init, absl::FunctionRef<float(float, float)> reduce_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<std::vector<float>>(); for (int64_t i = 0; i < cols; ++i) { float acc = init; for (int64_t j = 0; j < rows; ++j) { acc = reduce_function(acc, matrix(j, i)); } result->push_back(acc); } return result; } /*static*/ std::vector<float> ReferenceUtil::Reduce4DTo1D( const Array4D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { std::vector<float> result; CHECK_EQ(dims.size(), 3); const absl::flat_hash_set<int64_t> dim_set(dims.begin(), dims.end()); CHECK_EQ(dim_set.size(), 3); for (int64_t a0 = 0; a0 == 0 || (!dim_set.contains(0) && a0 < array.n1()); ++a0) { for (int64_t a1 = 0; a1 == 0 || (!dim_set.contains(1) && a1 < array.n2()); ++a1) { for (int64_t a2 = 0; a2 == 0 || (!dim_set.contains(2) && a2 < array.n3()); ++a2) { for (int64_t a3 = 0; a3 == 0 || (!dim_set.contains(3) && a3 < array.n4()); ++a3) { float accumulator = init; for (int64_t i0 = 0; i0 == 0 || (dim_set.contains(0) && i0 < array.n1()); ++i0) { for (int64_t i1 = 0; i1 == 0 || (dim_set.contains(1) && i1 < array.n2()); ++i1) { for (int64_t i2 = 0; i2 == 0 || (dim_set.contains(2) && i2 < array.n3()); ++i2) { for (int64_t i3 = 0; i3 == 0 || (dim_set.contains(3) && i3 < array.n4()); ++i3) { // Handle zero-sized arrays. if (array.n1() > 0 && array.n2() > 0 && array.n3() > 0 && array.n4() > 0) { accumulator = reduce_function( accumulator, array(a0 + i0, a1 + i1, a2 + i2, a3 + i3)); } } } } } result.push_back(accumulator); } } } } return result; } /* static */ std::unique_ptr<Array4D<float>> ReferenceUtil::Broadcast1DTo4D( const std::vector<float>& array, const std::vector<int64_t>& bounds, int64_t broadcast_from_dim) { auto result = std::make_unique<Array4D<float>>(bounds[0], bounds[1], bounds[2], bounds[3]); for (int64_t i = 0; i < result->n1(); ++i) { for (int64_t j = 0; j < result->n2(); ++j) { for (int64_t k = 0; k < result->n3(); ++k) { for (int64_t l = 0; l < result->n4(); ++l) { switch (broadcast_from_dim) { case 0: (*result)(i, j, k, l) = array[i]; break; case 1: (*result)(i, j, k, l) = array[j]; break; case 2: (*result)(i, j, k, l) = array[k]; break; case 3: (*result)(i, j, k, l) = array[l]; break; default: break; } } } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::Reduce3DTo2D( const Array3D<float>& array, float init, absl::Span<const int64_t> dims, absl::FunctionRef<float(float, float)> reduce_function) { CHECK_EQ(dims.size(), 1); int64_t rows = dims[0] == 0 ? array.n2() : array.n1(); int64_t cols = dims[0] == 2 ? array.n2() : array.n3(); auto result = std::make_unique<Array2D<float>>(rows, cols); result->Fill(init); for (int i0 = 0; i0 < array.n1(); ++i0) { for (int i1 = 0; i1 < array.n2(); ++i1) { for (int i2 = 0; i2 < array.n3(); ++i2) { int64_t row = dims[0] == 0 ? i1 : i0; int64_t col = dims[0] == 2 ? i1 : i2; (*result)(row, col) = reduce_function((*result)(row, col), array(i0, i1, i2)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j)); } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapArray2D( const Array2D<float>& lhs, const Array2D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.height(), rhs.height()); CHECK_EQ(lhs.width(), rhs.width()); int64_t rows = lhs.height(); int64_t cols = rhs.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(lhs(i, j), rhs(i, j)); } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& array, absl::FunctionRef<float(float)> map_function) { int64_t n1 = array.n1(); int64_t n2 = array.n2(); int64_t n3 = array.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(array(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array3D<float>> ReferenceUtil::MapArray3D( const Array3D<float>& lhs, const Array3D<float>& rhs, absl::FunctionRef<float(float, float)> map_function) { CHECK_EQ(lhs.n1(), rhs.n1()); CHECK_EQ(lhs.n2(), rhs.n2()); CHECK_EQ(lhs.n3(), rhs.n3()); int64_t n1 = lhs.n1(); int64_t n2 = rhs.n2(); int64_t n3 = rhs.n3(); auto result = std::make_unique<Array3D<float>>(n1, n2, n3); for (int64_t i = 0; i < n1; ++i) { for (int64_t j = 0; j < n2; ++j) { for (int64_t k = 0; k < n3; ++k) { (*result)(i, j, k) = map_function(lhs(i, j, k), rhs(i, j, k)); } } } return result; } /* static */ std::unique_ptr<Array2D<float>> ReferenceUtil::MapWithIndexArray2D( const Array2D<float>& matrix, absl::FunctionRef<float(float, int64_t, int64_t)> map_function) { int64_t rows = matrix.height(); int64_t cols = matrix.width(); auto result = std::make_unique<Array2D<float>>(rows, cols); for (int64_t i = 0; i < rows; ++i) { for (int64_t j = 0; j < cols; ++j) { (*result)(i, j) = map_function(matrix(i, j), i, j); } } return result; }
#include "xla/reference_util.h" #include <cmath> #include <memory> #include "xla/array2d.h" #include "xla/array3d.h" #include "xla/array4d.h" #include "xla/client/padding.h" #include "xla/literal.h" #include "xla/test.h" #include "xla/tests/literal_test_util.h" #include "xla/xla_data.pb.h" class ReferenceUtilTest : public ::testing::Test { protected: ReferenceUtilTest() { matrix_ = std::make_unique<Array2D<float>>(rows_, cols_); // [1.f 2.f 3.f] // [4.f 5.f 6.f] for (int64_t i = 0; i < rows_; ++i) { for (int64_t j = 0; j < cols_; ++j) { (*matrix_)(i, j) = i * cols_ + j + 1; } } } const int64_t rows_ = 2; const int64_t cols_ = 3; std::unique_ptr<Array2D<float>> matrix_; }; TEST_F(ReferenceUtilTest, TransposeArray2D) { auto result = ReferenceUtil::TransposeArray2D(*matrix_); auto actual_literal = LiteralUtil::CreateR2FromArray2D(*result); LiteralTestUtil::ExpectR2Near<float>({{1.f, 4.f}, {2.f, 5.f}, {3.f, 6.f}}, actual_literal, ErrorSpec(0.0001)); }
ShapeTreeTest_ArrayShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, ArrayShape) { ShapeTree<int> shape_tree{array_shape_}; *shape_tree.mutable_element({}) = 42; EXPECT_EQ(42, shape_tree.element({})); *shape_tree.mutable_element({}) = 123; EXPECT_EQ(123, shape_tree.element({})); EXPECT_TRUE(ShapeUtil::Compatible(array_shape_, shape_tree.shape())); // Test the copy constructor. ShapeTree<int> copy{shape_tree}; EXPECT_EQ(123, copy.element({})); // Mutate the copy, and ensure the original doesn't change. *copy.mutable_element({}) = 99; EXPECT_EQ(99, copy.element({})); EXPECT_EQ(123, shape_tree.element({})); // Test the assignment operator. copy = shape_tree; EXPECT_EQ(123, copy.element({})); }
ShapeTreeTest_ConstFind
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, ConstFind) { const ShapeTree<int> t(nested_tuple_shape_, 42); auto found = t.find({1, 0}); EXPECT_NE(found, t.end()); // The found key must be the same key we searched for. EXPECT_EQ(found->first, ShapeIndex({1, 0})); // The 'ShapeTree' has 42 at every position. EXPECT_EQ(found->second, 42); }
ShapeTreeTest_ConstIterate
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, ConstIterate) { const ShapeTree<int> t(nested_tuple_shape_, 42); int num_nodes = 0; for (const auto& index_to_data : t) { EXPECT_EQ(42, index_to_data.second); ++num_nodes; } EXPECT_EQ(10, num_nodes); }
ShapeTreeTest_ConstructWithPointerToShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, ConstructWithPointerToShape) { // Construct a ShapeTree using a pointer to a shape, rather than a reference // to a shape. This constructor is an optimization to let us avoid // constructing and destroying temporary shapes when we have many ShapeTrees. ShapeTree<int> t(&nested_tuple_shape_, 42); int num_nodes = 0; t.ForEachElement([&num_nodes](const ShapeIndex& /*index*/, int data) { EXPECT_EQ(42, data); ++num_nodes; }); EXPECT_EQ(10, num_nodes); }
ShapeTreeTest_CopyAssignWithPointerToShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, CopyAssignWithPointerToShape) { ShapeTree<int> source(&nested_tuple_shape_, 0); ShapeTree<int> dest; dest = source; EXPECT_EQ(&dest.shape(), &nested_tuple_shape_); }
ShapeTreeTest_CopySubtreeFromArrayShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, CopySubtreeFromArrayShape) { // Test CopySubtreeFrom method for a single value copied between array-shaped // ShapeTrees. ShapeTree<int> source(array_shape_); *source.mutable_element(/*index=*/{}) = 42; ShapeTree<int> destination(array_shape_, 123); EXPECT_EQ(destination.element(/*index=*/{}), 123); destination.CopySubtreeFrom(source, /*source_base_index=*/{}, /*target_base_index=*/{}); EXPECT_EQ(destination.element(/*index=*/{}), 42); }
ShapeTreeTest_CopySubtreeFromNestedShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, CopySubtreeFromNestedShape) { // Test CopySubtreeFrom method for a copy from a nested-tuple-shape. ShapeTree<int> source(nested_tuple_shape_, 42); *source.mutable_element(/*index=*/{1}) = 10; *source.mutable_element(/*index=*/{1, 0}) = 11; *source.mutable_element(/*index=*/{1, 1}) = 12; ShapeTree<int> destination( ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), 0); destination.CopySubtreeFrom(source, /*source_base_index=*/{1}, /*target_base_index=*/{}); EXPECT_EQ(destination.element(/*index=*/{}), 10); EXPECT_EQ(destination.element(/*index=*/{0}), 11); EXPECT_EQ(destination.element(/*index=*/{1}), 12); }
ShapeTreeTest_CopySubtreeIntoNestedShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, CopySubtreeIntoNestedShape) { // Test CopySubtreeFrom method for a copy of a tuple-shaped ShapeTree into a // nested-tuple-shaped ShapeTree. ShapeTree<int> source( ShapeUtil::MakeTupleShape({array_shape_, array_shape_})); *source.mutable_element(/*index=*/{}) = 10; *source.mutable_element(/*index=*/{0}) = 11; *source.mutable_element(/*index=*/{1}) = 12; ShapeTree<int> destination(nested_tuple_shape_, 0); destination.CopySubtreeFrom(source, /*source_base_index=*/{}, /*target_base_index=*/{2, 0}); EXPECT_EQ(destination.element(/*index=*/{}), 0); EXPECT_EQ(destination.element(/*index=*/{0}), 0); EXPECT_EQ(destination.element(/*index=*/{1}), 0); EXPECT_EQ(destination.element(/*index=*/{1, 0}), 0); EXPECT_EQ(destination.element(/*index=*/{1, 1}), 0); EXPECT_EQ(destination.element(/*index=*/{2}), 0); EXPECT_EQ(destination.element(/*index=*/{2, 0}), 10); EXPECT_EQ(destination.element(/*index=*/{2, 0, 0}), 11); EXPECT_EQ(destination.element(/*index=*/{2, 0, 1}), 12); EXPECT_EQ(destination.element(/*index=*/{2, 1}), 0); }
ShapeTreeTest_CopyWithPointerToShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, CopyWithPointerToShape) { ShapeTree<int> source(&nested_tuple_shape_, 0); ShapeTree<int> dest(source); EXPECT_EQ(&dest.shape(), &nested_tuple_shape_); }
ShapeTreeTest_DefaultConstructor
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, DefaultConstructor) { ShapeTree<int> int_tree; EXPECT_TRUE(ShapeUtil::IsEmptyTuple(int_tree.shape())); ShapeTree<bool> bool_tree; EXPECT_TRUE(ShapeUtil::IsEmptyTuple(bool_tree.shape())); }
ShapeTreeTest_EmptyTupleMustHaveNoLeaves
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, EmptyTupleMustHaveNoLeaves) { ShapeTree<int> shape_tree{ShapeUtil::MakeTupleShape({})}; EXPECT_EQ(0, shape_tree.leaf_count()); }
ShapeTreeTest_Find
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, Find) { ShapeTree<int> t(nested_tuple_shape_, 42); auto found = t.find({1, 0}); EXPECT_NE(found, t.end()); // The found key must be the same key we searched for. EXPECT_EQ(found->first, ShapeIndex({1, 0})); // The 'ShapeTree' has 42 at every position. EXPECT_EQ(found->second, 42); }
ShapeTreeTest_FullCopySubtreeFromTupleShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, FullCopySubtreeFromTupleShape) { // Test CopySubtreeFrom method for a copy of all elements from one // tuple-shaped ShapeTree to another. ShapeTree<int> source(tuple_shape_); *source.mutable_element(/*index=*/{}) = 10; *source.mutable_element(/*index=*/{0}) = 11; *source.mutable_element(/*index=*/{1}) = 12; *source.mutable_element(/*index=*/{2}) = 13; ShapeTree<int> destination(tuple_shape_, 0); destination.CopySubtreeFrom(source, /*source_base_index=*/{}, /*target_base_index=*/{}); EXPECT_EQ(destination.element(/*index=*/{}), 10); EXPECT_EQ(destination.element(/*index=*/{0}), 11); EXPECT_EQ(destination.element(/*index=*/{1}), 12); EXPECT_EQ(destination.element(/*index=*/{2}), 13); }
ShapeTreeTest_InitValueConstructor
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, InitValueConstructor) { TestInitValueConstructor(array_shape_, 1); TestInitValueConstructor(tuple_shape_, 4); TestInitValueConstructor(nested_tuple_shape_, 10); }
ShapeTreeTest_InvalidIndexingNestedTuple
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, InvalidIndexingNestedTuple) { ShapeTree<int> shape_tree{nested_tuple_shape_}; #ifndef NDEBUG EXPECT_DEATH(shape_tree.element({0, 0}), ""); #endif }
ShapeTreeTest_InvalidIndexingTuple
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, InvalidIndexingTuple) { ShapeTree<int> shape_tree{tuple_shape_}; #ifndef NDEBUG EXPECT_DEATH(shape_tree.element({4}), ""); #endif }
ShapeTreeTest_IterateAndMutate
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, IterateAndMutate) { ShapeTree<int> t(nested_tuple_shape_, 42); int i = 0; for (auto& index_to_data : t) { EXPECT_EQ(42, index_to_data.second); if (i == 1) { index_to_data.second = 98; } ++i; } (*t.begin()).second = 78; EXPECT_EQ(78, (*t.begin()).second); i = 0; for (auto& index_to_data : t) { if (i == 0) { EXPECT_EQ(78, index_to_data.second); } else if (i == 1) { EXPECT_EQ(98, index_to_data.second); } else { EXPECT_EQ(42, index_to_data.second); } ++i; } EXPECT_EQ(78, (*t.begin()).second); EXPECT_EQ(98, (*std::next(t.begin())).second); }
ShapeTreeTest_IterateOrder
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, IterateOrder) { ShapeTree<int> t(nested_tuple_shape_, 42); std::vector<ShapeIndex> v; v.reserve(t.leaf_count()); for (auto index_to_data : t) { v.push_back(index_to_data.first); } EXPECT_EQ(v, (std::vector<ShapeIndex>{{}, {0}, {1}, {1, 0}, {1, 1}, {2}, {2, 0}, {2, 0, 0}, {2, 0, 1}, {2, 1}})); }
ShapeTreeTest_IterateOrderLeaves
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, IterateOrderLeaves) { ShapeTree<int> t(nested_tuple_shape_, 42); std::vector<ShapeIndex> v; const auto& leaves = t.leaves(); v.reserve(t.leaf_count()); for (auto index_to_data : leaves) { v.push_back(index_to_data.first); } EXPECT_EQ(v, (std::vector<ShapeIndex>{ {0}, {1, 0}, {1, 1}, {2, 0, 0}, {2, 0, 1}, {2, 1}})); }
ShapeTreeTest_IterateSimple
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, IterateSimple) { ShapeTree<int> t(nested_tuple_shape_, 42); int num_nodes = 0; for (auto index_to_data : t) { EXPECT_EQ(42, index_to_data.second); ++num_nodes; } EXPECT_EQ(10, num_nodes); }
ShapeTreeTest_NestedEmptyTuple
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, NestedEmptyTuple) { Shape shape( ShapeUtil::MakeTupleShape({ShapeUtil::MakeTupleShape({}), array_shape_})); ShapeTree<int> shape_tree{shape}; EXPECT_EQ(ShapeUtil::GetLeafCount(shape), shape_tree.leaf_count()); }
ShapeTreeTest_NestedTupleShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, NestedTupleShape) { ShapeTree<int> shape_tree{nested_tuple_shape_}; *shape_tree.mutable_element({0}) = 42; *shape_tree.mutable_element({1, 1}) = 123; *shape_tree.mutable_element({2, 0, 1}) = -100; EXPECT_EQ(42, shape_tree.element({0})); EXPECT_EQ(123, shape_tree.element({1, 1})); EXPECT_EQ(-100, shape_tree.element({2, 0, 1})); EXPECT_TRUE(ShapeUtil::Compatible(nested_tuple_shape_, shape_tree.shape())); // Test the copy constructor. ShapeTree<int> copy{shape_tree}; EXPECT_EQ(42, copy.element({0})); EXPECT_EQ(123, copy.element({1, 1})); EXPECT_EQ(-100, copy.element({2, 0, 1})); // Mutate the copy, and ensure the original doesn't change. *copy.mutable_element({0}) = 1; *copy.mutable_element({1, 1}) = 2; *copy.mutable_element({2, 0, 1}) = 3; EXPECT_EQ(1, copy.element({0})); EXPECT_EQ(2, copy.element({1, 1})); EXPECT_EQ(3, copy.element({2, 0, 1})); EXPECT_EQ(42, shape_tree.element({0})); EXPECT_EQ(123, shape_tree.element({1, 1})); EXPECT_EQ(-100, shape_tree.element({2, 0, 1})); // Test the assignment operator. copy = shape_tree; EXPECT_EQ(42, copy.element({0})); EXPECT_EQ(123, copy.element({1, 1})); EXPECT_EQ(-100, copy.element({2, 0, 1})); }
ShapeTreeTest_OperatorEquals
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, OperatorEquals) { { ShapeTree<int> a(array_shape_, 123); ShapeTree<int> b(array_shape_, 42); ShapeTree<int> c(array_shape_, 42); EXPECT_FALSE(a == b); EXPECT_TRUE(a != b); EXPECT_TRUE(b == c); } { ShapeTree<int> a(tuple_shape_); *a.mutable_element(/*index=*/{}) = 10; *a.mutable_element(/*index=*/{0}) = 11; *a.mutable_element(/*index=*/{1}) = 12; ShapeTree<int> b(tuple_shape_); *b.mutable_element(/*index=*/{}) = 10; *b.mutable_element(/*index=*/{0}) = 42; *b.mutable_element(/*index=*/{1}) = 11; ShapeTree<int> c(tuple_shape_); *c.mutable_element(/*index=*/{}) = 10; *c.mutable_element(/*index=*/{0}) = 42; *c.mutable_element(/*index=*/{1}) = 11; EXPECT_FALSE(a == b); EXPECT_TRUE(a != b); EXPECT_TRUE(b == c); EXPECT_FALSE(b != c); } }
ShapeTreeTest_ReverseIterateOrder
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, ReverseIterateOrder) { ShapeTree<int> t(nested_tuple_shape_, 42); std::vector<ShapeIndex> v; v.reserve(t.leaf_count()); for (auto it = t.rbegin(); it != t.rend(); ++it) { v.push_back(it->first); } EXPECT_EQ(v, (std::vector<ShapeIndex>{ {2, 1}, {2, 0, 1}, {2, 0, 0}, {2, 0}, {2}, {1, 1}, {1, 0}, {1}, {0}, {}, })); }
ShapeTreeTest_ReverseIterateOrderLeaves
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, ReverseIterateOrderLeaves) { ShapeTree<int> t(nested_tuple_shape_, 42); std::vector<ShapeIndex> v; v.reserve(t.leaf_count()); for (auto it = t.leaf_rbegin(); it != t.leaf_rend(); ++it) { v.push_back(it->first); } EXPECT_EQ(v, (std::vector<ShapeIndex>{ {2, 1}, {2, 0, 1}, {2, 0, 0}, {1, 1}, {1, 0}, {0}, })); }
ShapeTreeTest_ShapeConstructor
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, ShapeConstructor) { TestShapeConstructor(array_shape_, 1); TestShapeConstructor(tuple_shape_, 4); TestShapeConstructor(nested_tuple_shape_, 10); }
ShapeTreeTest_ShapeTreeOfNonCopyableType
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, ShapeTreeOfNonCopyableType) { ShapeTree<std::unique_ptr<int>> shape_tree{tuple_shape_}; EXPECT_EQ(shape_tree.element({2}).get(), nullptr); *shape_tree.mutable_element({2}) = std::make_unique<int>(42); EXPECT_EQ(*shape_tree.element({2}), 42); }
ShapeTreeTest_SingleElementCopySubtreeFromTupleShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, SingleElementCopySubtreeFromTupleShape) { // Test CopySubtreeFrom method for a copy of a single element from one // tuple-shaped ShapeTree to another. ShapeTree<int> source(tuple_shape_); *source.mutable_element(/*index=*/{}) = 10; *source.mutable_element(/*index=*/{0}) = 11; *source.mutable_element(/*index=*/{1}) = 12; *source.mutable_element(/*index=*/{2}) = 13; ShapeTree<int> destination(tuple_shape_, 0); destination.CopySubtreeFrom(source, /*source_base_index=*/{0}, /*target_base_index=*/{1}); EXPECT_EQ(destination.element(/*index=*/{}), 0); EXPECT_EQ(destination.element(/*index=*/{0}), 0); EXPECT_EQ(destination.element(/*index=*/{1}), 11); EXPECT_EQ(destination.element(/*index=*/{2}), 0); }
ShapeTreeTest_TupleShape
xla/shape_tree_test.cc
IndexTable::IndexTable(const Shape& shape) : entries_(1) { size_t next_node_id = 0; CreateEntry(entries_[0], shape, next_node_id); } void IndexTable::CreateEntry(Entry& entry, const Shape& shape, size_t& next_node_id) { entry.node_id = next_node_id++; if (!shape.IsTuple()) return; // The nodes are in depth-first pre-order. However, in order to efficiently // lookup indices, we generate the index table using breadth-first. size_t children_start_id = entries_.size(); entry.children_start_id = children_start_id; // Add entry for children first, before recursing, so they are consecutive. entries_.resize(entries_.size() + shape.tuple_shapes_size()); for (size_t i = 0; i < shape.tuple_shapes_size(); ++i) { CreateEntry(entries_[children_start_id + i], shape.tuple_shapes(i), next_node_id); } } const IndexTable::Entry& IndexTable::operator[](ShapeIndexView index) const { const Entry* result = &entries_.front(); for (int64_t i : index) { CHECK_GE(result->children_start_id, 0); result = &entries_[result->children_start_id + i]; } return *result; }
#include "xla/shape_tree.h" #include <iterator> #include <memory> #include <utility> #include <vector> #include "xla/shape.h" #include "xla/shape_util.h" #include "xla/test.h" #include "xla/xla_data.pb.h" #include "tsl/platform/test_benchmark.h" class ShapeTreeTest : public ::testing::Test { protected: ShapeTreeTest() { array_shape_ = ShapeUtil::MakeShape(F32, {42, 42, 123}); tuple_shape_ = ShapeUtil::MakeTupleShape({array_shape_, array_shape_, array_shape_}); nested_tuple_shape_ = ShapeUtil::MakeTupleShape( {array_shape_, ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), ShapeUtil::MakeTupleShape( {ShapeUtil::MakeTupleShape({array_shape_, array_shape_}), array_shape_})}); } void TestShapeConstructor(const Shape& shape, int expected_num_nodes); void TestInitValueConstructor(const Shape& shape, int expected_num_nodes); // An array shape (non-tuple). Shape array_shape_; // A three element tuple shape. Shape tuple_shape_; // A nested tuple shape of the following form: (a, (c, d), ((e, f), g)) Shape nested_tuple_shape_; }; TEST_F(ShapeTreeTest, TupleShape) { ShapeTree<int> shape_tree{tuple_shape_}; *shape_tree.mutable_element({}) = 1; *shape_tree.mutable_element({0}) = 42; *shape_tree.mutable_element({1}) = 123; *shape_tree.mutable_element({2}) = -100; EXPECT_EQ(1, shape_tree.element({})); EXPECT_EQ(42, shape_tree.element({0})); EXPECT_EQ(123, shape_tree.element({1})); EXPECT_EQ(-100, shape_tree.element({2})); EXPECT_TRUE(ShapeUtil::Compatible(tuple_shape_, shape_tree.shape())); // Sum all elements in the shape. int sum = 0; shape_tree.ForEachElement( [&sum](const ShapeIndex& /*index*/, int data) { sum += data; }); EXPECT_EQ(66, sum); // Test the copy constructor. ShapeTree<int> copy{shape_tree}; EXPECT_EQ(1, copy.element({})); EXPECT_EQ(42, copy.element({0})); EXPECT_EQ(123, copy.element({1})); EXPECT_EQ(-100, copy.element({2})); // Write zero to all data elements. shape_tree.ForEachMutableElement( [](const ShapeIndex& /*index*/, int* data) { *data = 0; }); EXPECT_EQ(0, shape_tree.element({})); EXPECT_EQ(0, shape_tree.element({0})); EXPECT_EQ(0, shape_tree.element({1})); EXPECT_EQ(0, shape_tree.element({2})); EXPECT_EQ(1, copy.element({})); EXPECT_EQ(42, copy.element({0})); EXPECT_EQ(123, copy.element({1})); EXPECT_EQ(-100, copy.element({2})); // Test the assignment operator. copy = shape_tree; EXPECT_EQ(0, copy.element({})); EXPECT_EQ(0, copy.element({0})); EXPECT_EQ(0, copy.element({1})); EXPECT_EQ(0, copy.element({2})); }
TestNumberTest_RoundTrip
xla/python/ifrt/serdes_test.cc
Registry* registry() { static auto* r = new Registry(); return r; } void RegisterSerDes(const void* type_id, std::unique_ptr<SerDes> serdes) { Registry* const r = registry(); absl::MutexLock l(&r->mu); CHECK(r->type_id_to_serdes.insert({type_id, serdes.get()}).second) << "xla::ifrt::SerDes cannot be registered more than once for the same " "type id: " << type_id; const absl::string_view name = serdes->type_name(); CHECK(r->name_to_serdes.insert({name, serdes.get()}).second) << "xla::ifrt::SerDes cannot be registered more than once for the same " "name: " << name; // `SerDes` must be kept alive until the process exit. Since global variables // should not have destructors, we can just release the unique ptr. serdes.release(); } absl::StatusOr<Serialized> Serialize(Serializable& serializable) { SerDes* serdes; { Registry* const r = registry(); absl::MutexLock l(&r->mu); auto it = r->type_id_to_serdes.find(serializable.dynamicClassID()); if (it == r->type_id_to_serdes.end()) { return absl::UnimplementedError( "Serialize call failed. Serializable has no associated SerDes " "implementation"); } serdes = it->second; } TF_ASSIGN_OR_RETURN(std::string data, serdes->Serialize(serializable)); Serialized proto; proto.set_type_name(std::string(serdes->type_name())); proto.set_data(std::move(data)); return proto; } absl::StatusOr<std::unique_ptr<Serializable>> DeserializeUnchecked( const Serialized& serialized, std::unique_ptr<DeserializeOptions> options) { SerDes* serdes; { Registry* const r = registry(); absl::MutexLock l(&r->mu); auto it = r->name_to_serdes.find(serialized.type_name()); if (it == r->name_to_serdes.end()) { return absl::UnimplementedError(absl::StrCat( "Deserialize call failed. Serializable has no associated SerDes ", "implementation. type_name: ", serialized.type_name())); } serdes = it->second; } return serdes->Deserialize(serialized.data(), std::move(options)); }
#include "xla/python/ifrt/serdes.h" #include <memory> #include <string> #include <utility> #include <gmock/gmock.h> #include <gtest/gtest.h> #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/numbers.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ExtensibleRTTI.h" #include "xla/python/ifrt/serdes.pb.h" #include "tsl/platform/errors.h" #include "tsl/platform/status_matchers.h" #include "tsl/platform/statusor.h" class TestNumberTest : public testing::Test { protected: static void SetUpTestSuite() { RegisterSerDes<TestNumber>(std::make_unique<TestNumberSerDes>()); } }; TEST_F(TestNumberTest, RoundTrip) { auto obj = std::make_unique<TestNumber>(1234); TF_ASSERT_OK_AND_ASSIGN(Serialized serialized, Serialize(*obj)); TF_ASSERT_OK_AND_ASSIGN( auto deserialized, Deserialize<TestNumber>(serialized, /*options=*/nullptr)); EXPECT_EQ(obj->number, deserialized->number); }
TestNumberTest_WithOptions
xla/python/ifrt/serdes_test.cc
Registry* registry() { static auto* r = new Registry(); return r; } void RegisterSerDes(const void* type_id, std::unique_ptr<SerDes> serdes) { Registry* const r = registry(); absl::MutexLock l(&r->mu); CHECK(r->type_id_to_serdes.insert({type_id, serdes.get()}).second) << "xla::ifrt::SerDes cannot be registered more than once for the same " "type id: " << type_id; const absl::string_view name = serdes->type_name(); CHECK(r->name_to_serdes.insert({name, serdes.get()}).second) << "xla::ifrt::SerDes cannot be registered more than once for the same " "name: " << name; // `SerDes` must be kept alive until the process exit. Since global variables // should not have destructors, we can just release the unique ptr. serdes.release(); } absl::StatusOr<Serialized> Serialize(Serializable& serializable) { SerDes* serdes; { Registry* const r = registry(); absl::MutexLock l(&r->mu); auto it = r->type_id_to_serdes.find(serializable.dynamicClassID()); if (it == r->type_id_to_serdes.end()) { return absl::UnimplementedError( "Serialize call failed. Serializable has no associated SerDes " "implementation"); } serdes = it->second; } TF_ASSIGN_OR_RETURN(std::string data, serdes->Serialize(serializable)); Serialized proto; proto.set_type_name(std::string(serdes->type_name())); proto.set_data(std::move(data)); return proto; } absl::StatusOr<std::unique_ptr<Serializable>> DeserializeUnchecked( const Serialized& serialized, std::unique_ptr<DeserializeOptions> options) { SerDes* serdes; { Registry* const r = registry(); absl::MutexLock l(&r->mu); auto it = r->name_to_serdes.find(serialized.type_name()); if (it == r->name_to_serdes.end()) { return absl::UnimplementedError(absl::StrCat( "Deserialize call failed. Serializable has no associated SerDes ", "implementation. type_name: ", serialized.type_name())); } serdes = it->second; } return serdes->Deserialize(serialized.data(), std::move(options)); }
#include "xla/python/ifrt/serdes.h" #include <memory> #include <string> #include <utility> #include <gmock/gmock.h> #include <gtest/gtest.h> #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/numbers.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ExtensibleRTTI.h" #include "xla/python/ifrt/serdes.pb.h" #include "tsl/platform/errors.h" #include "tsl/platform/status_matchers.h" #include "tsl/platform/statusor.h" class TestNumberTest : public testing::Test { protected: static void SetUpTestSuite() { RegisterSerDes<TestNumber>(std::make_unique<TestNumberSerDes>()); } }; TEST_F(TestNumberTest, WithOptions) { auto obj = std::make_unique<TestNumber>(1234); TF_ASSERT_OK_AND_ASSIGN(Serialized serialized, Serialize(*obj)); auto options = std::make_unique<TestNumberDeserializeOptions>(); options->injected_failure = absl::InternalError("injected failure"); EXPECT_THAT(Deserialize<TestNumber>(serialized, std::move(options)), StatusIs(absl::StatusCode::kInternal, "injected failure")); }